簡體   English   中英

冒泡排序選項。 需要添加一個打印氣泡位移的(print_bubble)程序

[英]Bubble sort options. Need to add a (print_bubble) procedure that prints the bubble displacement

我已經嘗試了幾件事,但它只是不起作用,我剛從 C 開始。 我還想為完成的交換次數添加一個計數器,但無法找到寫它的地方。 任何幫助表示贊賞!

#include <stdio.h> 

void swap(int *xp, int *yp) 
{ 
    int temp = *xp; 
    *xp = *yp; 
    *yp = temp; 
} 

// A function to implement bubble sort 
void bubbleSort(int arr[], int n) 
{ 
   int i, j; 
   for (i = 0; i < n-1; i++)       

       // Last i elements are already in place    
       for (j = 0; j < n-i-1; j++)  
           if (arr[j] > arr[j+1]) 
              swap(&arr[j], &arr[j+1]); 
} 

/* Function to print an array */
void printArray(int arr[], int size) 
{ 
    int i; 
    for (i=0; i < size; i++) 
        printf("%d ", arr[i]); 
    printf("\n"); 
} 



// Driver program to test above functions 
int main() 
{ 
    int arr[] = {64, 34, 25, 12, 22, 11, 90}; 
    int n = sizeof(arr)/sizeof(arr[0]); 
    bubbleSort(arr, n); 
    printf("Sorted array: \n"); 
    printArray(arr, n); 
    return 0; 
} 

如果您使用{}來划分范圍,則很容易找到計算交換的位置。

void bubbleSort(int arr[], int n) 
{ 
   int i, j; 
   for (i = 0; i < n-1; i++)       
   {
       for (j = 0; j < n-i-1; j++)
       {
           if (arr[j] > arr[j+1]) 
           {
              swap(&arr[j], &arr[j+1]); 
              // increase swap count here
           }
       }
   }
}

hackish 方法是簡單地添加一個全局size_t nswaps並增加nswaps++; swap()中。

將指針傳遞給計數器

更正確的方法是,如果您無法從void更改bubblesort()的返回類型,則添加一個參數,該參數允許將指向計數器的指針傳遞給bubblesort (... size_t *nswaps) ,然后在您的if (arr[j] > arr[j+1]) {...; (*nswaps)++; } if (arr[j] > arr[j+1]) {...; (*nswaps)++; } if (arr[j] > arr[j+1]) {...; (*nswaps)++; } 例如

// A function to implement bubble sort 
void bubbleSort(int arr[], int n, size_t *nswaps) 
{ 
    int i, j; 
    for (i = 0; i < n-1; i++)       

    // Last i elements are already in place    
    for (j = 0; j < n-i-1; j++)  
        if (arr[j] > arr[j+1]) {
            swap(&arr[j], &arr[j+1]);
            (*nswaps)++;                     /* increment counter */
        }
} 

然后在main()中將您的計數器初始化為零並通過使用'&' (地址)運算符將指針傳遞給計數器,在bubblesort()中增加該地址處的值,然后您可以使用更新后的值返回main()

// Driver program to test above functions 
int main (void) 
{ 
    int arr[] = {64, 34, 25, 12, 22, 11, 90}; 
    int n = sizeof(arr)/sizeof(arr[0]); 
    size_t nswaps = 0;                            /* add counter */
    bubbleSort(arr, n, &nswaps); 
    printf("Sorted array in (%zu swaps): \n", nswaps); 
    printArray(arr, n); 
    return 0; 
} 

提供有意義的計數返回

您還可以將bubblesort()的返回類型更改為size_t並簡單地返回計數,例如

// A function to implement bubble sort 
size_t bubbleSort(int arr[], int n) 
{ 
    size_t nswaps = 0;
    int i, j; 
    for (i = 0; i < n-1; i++)       

    // Last i elements are already in place    
    for (j = 0; j < n-i-1; j++)  
        if (arr[j] > arr[j+1]) {
            swap(&arr[j], &arr[j+1]);
            nswaps++; 
        }

    return nswaps;
} 

然后在main()中捕獲返回:

// Driver program to test above functions 
int main() 
{ 
    int arr[] = {64, 34, 25, 12, 22, 11, 90}; 
    int n = sizeof(arr)/sizeof(arr[0]); 
    size_t nswaps = 0;
    nswaps = bubbleSort(arr, n); 
    printf("Sorted array in (%zu swaps): \n", nswaps); 
    printArray(arr, n); 
    return 0; 
} 

暫無
暫無

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

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