如何计算冒泡排序中的交换次数?

How to count number of swaps in a bubble sort?

所以我需要我的程序来打印输入的值并计算交换次数(不是比较)。到目前为止,除了交换计数器外,我一切正常。我尝试通过在我的 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;
}

您已经在设置 ​​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); 

像@ProfOak 建议的那样摆脱你的 while 循环,你就设置好了。

你有一个 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;
}

Swift 4 版本:

func countSwaps(a: [Int]) -> Void {

   var result = a
   var numberOfSwaps = 0
   let length = a.count

   for i in 0 ..< length - 1 {
      for j in 0 ..< length - 1 - i {
         if result[j] > result[j + 1] {
            result.swapAt(j, j + 1)
            numberOfSwaps += 1
         }
      }
   }

   print("Array is sorted in \(numberOfSwaps) swaps.")
   print("First Element: \(result.first!)")
   print("Last Element: \(result.last!)")
}

升序排列:

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

所以要计算一个元素的交换次数,只需要计算右边比它小的元素的个数即可。