如何统计冒泡排序中的掉期数量?

时间:2015-03-26 20:37:36

标签: c arrays function sorting swap

所以我需要我的程序打印输入的值并计算掉期数(而不是比较)。到目前为止,除了交换计数器外,我还能正常工作我尝试在if语句中使用swap++;以及冒泡排序来增加,但这不起作用。有任何想法吗?这是我的代码。

#include <stdio.h>

int sort(int array[], int count);

int main(void) {

    int numArray[100];
    int counter, value;

    printf("Enter array length \n");
    scanf("%d", &counter); 

    int i = 0;
    while(i < counter){
        scanf("%d", &numArray[i]);
        i++;    
    }

    i = 0;
    while(i < counter) {
        sort(numArray, counter);
        i++;
    }

    int totalSwaps = sort(numArray, counter);
    printf("Swaps: %d\n", totalSwaps); 

    i = 0;
    while(i < counter) {
        printf("Values: %d\n", numArray[i]); 
        i++;
    }

    return 0;
}

int sort(int array[], int count) {
    int i, j, temp;
    int swaps = 0;
    for(i = 0; i < count-1; ++i) {
        for(j=0; j < count-1-i; ++j) {
            if(array[j] > array[j+1]) {
                temp = array[j+1];
                array[j+1] = array[j];
                array[j] = temp;
                swaps++;
            }
        }
    }

    return swaps;
}

4 个答案:

答案 0 :(得分:2)

你有一个while循环来对其进行排序count次。您只需要运行一次排序功能,除非它没有第一次排序。

#include <stdio.h>

int sort(int array[], int count);

int main(void){

    int numArray[100];
    int counter;

    printf("Enter array length \n");
    scanf("%d", &counter); 

    int i;
    for (i = 0; i < counter; i++){
        printf("%d. Enter a numner: ", i);
        scanf("%d", &numArray[i]);
    }

    // How many times would you like to sort this array?
    // You only need one sort
    /*
    i = 0;
    while(i < counter){
        sort(numArray, counter);
        i++;
    }
    */

    int totalSwaps = sort(numArray, counter);

    if (totalSwaps == 0) {
        printf("The array is already in sorted order\n");
        return 0;
    }

    printf("Swaps: %d\n", totalSwaps); 

    for (i = 0; i < counter; i++) {
        printf("Values: %d\n", numArray[i]); 
    }
    return 0;
}



int sort(int array[], int count){

    int i, j, temp;
    int swaps = 0;
    for(i = 0; i < count-1; ++i){

        for(j=0; j<count-1-i; ++j){

            if(array[j] > array[j+1]){

                temp = array[j+1];
                array[j+1] = array[j];
                array[j] = temp;
                swaps++;
            }
        }
    }

    return swaps;
}

答案 1 :(得分:0)

您已经在设置totalSwaps的值时对数组进行了排序。

i = 0;
while(i < counter){
    sort(numArray, counter); // you're already sorting the array here
    i++;
}

int totalSwaps = sort(numArray, counter); --> the array is already sorted!
printf("Swaps: %d\n", totalSwaps); 

摆脱你的while循环,就像建议的@ProfOak一样。

答案 2 :(得分:0)

Swift 4版本:

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Handheld_default",
            "Handheld/{controller}/{action}/{id}",
            new { action = "Index", id = UrlParameter.Optional },
            namespaces: new[] { typeof(Areas.Handheld.Controllers.HomeController).Namespace }
        );
    }

答案 3 :(得分:0)

升序:

在冒泡排序中,最大的元素向右移动。因此,当在右侧找到较小的元素时,便完成了交换。

因此,要计算元素的交换数量,只需计算右侧小于该元素的元素的数量即可。

相关问题