簡體   English   中英

如何計算冒泡排序中的交換次數?

[英]How to count number of swaps in a bubble sort?

所以我需要我的程序打印輸入的值並計算交換次數(不是比較)。 到目前為止,除了交換計數器之外,我的一切都在工作。 我嘗試通過使用swap++;增加swap++; 在我的 if 語句和冒泡排序中,但這不起作用。 有任何想法嗎? 這是我的代碼。

#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;
}

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

按升序排列:

在冒泡排序中,最大的元素向右移動。 因此,當在右側找到較小的元素時,交換就完成了。

所以要計算一個元素的交換次數,只需計算右側小於它的元素數。

您已經在設置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 循環,你就准備好了。

斯威夫特 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!)")
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM