【问题标题】:How to count number of swaps in a bubble sort?如何计算冒泡排序中的交换次数?
【发布时间】:2015-03-26 20:37:36
【问题描述】:

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

【问题讨论】:

    标签: c arrays function sorting swap


    【解决方案1】:

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

    【讨论】:

    • 好的,谢谢。如果我想计算调用“排序”函数的次数,我该怎么做?
    • @Cryptic 你只需调用一次sort()
    • int totalSwaps = sort(numArray, counter); 调用一次 :-)
    • 我的意思是如果不使用sort,我怎么能终止程序? @ProfOak。
    • 为什么要运行一个不排序的排序程序?
    【解决方案2】:

    按升序排列:

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

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

    【讨论】:

      【解决方案3】:

      在设置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 循环,你就准备好了。

      【讨论】:

      • sort() 第一次对整数数组进行排序。因此,您有效地将 0 添加到 totalSwaps counter-1 次数。
      • @ProfOak 你说的很对,我不知道你为什么要在循环中调用排序方法......这真是漫长的一天。
      【解决方案4】:

      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!)")
      }
      

      【讨论】:

        猜你喜欢
        • 2016-04-04
        • 2021-02-06
        • 1970-01-01
        • 2012-07-05
        • 2013-03-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-05
        相关资源
        最近更新 更多