【问题标题】:Bubble sort options. Need to add a (print_bubble) procedure that prints the bubble displacement冒泡排序选项。需要添加一个打印气泡位移的(print_bubble)程序
【发布时间】:2020-01-31 21:44:27
【问题描述】:

我已经尝试了几件事,但它只是不起作用,我只是从 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; 
} 

【问题讨论】:

  • 在合适的地方添加int counter = 0;++counter;
  • “气泡置换”具体是什么意思?这是你比较的地方吗?
  • @Caleth “气泡置换”是指程序进行的交换次数。总数,对不起,如果我有点多余,英语不是我的主要语言。

标签: c arrays sorting


【解决方案1】:

如果您使用{} 来划分范围,很容易找到计算交换的地方。

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

【讨论】:

    【解决方案2】:

    hackish 方法是简单地添加一个全局size_t nswaps 并在swap() 中增加nswaps++;

    将指针传递给计数器

    如果您无法将bubblesort() 的返回类型从void 更改,则更正确的方法是添加一个参数,该参数允许将指向计数器的指针传递给bubblesort (... size_t *nswaps),然后在您的if (arr[j] &gt; 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() 中将您的计数器初始化为零并使用'&amp;'(地址)运算符将指针传递给计数器,在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; 
    } 
    

    【讨论】:

    • 非常感谢,我真的不能感谢你!它就像我希望的那样工作,现在,我遇到了一些问题,我编写了一个部分,以便可以打印冒泡排序的每次迭代。但是,我需要在每次打印的左侧指定变量“j”,在另一侧指定变量“i”(正在交换的那些)。我几乎可以肯定我写错了...``````
    • 不客气。如果您当前的排序问题与您有关,您可以在此处添加评论,我会尽力提供帮助。如果这是一个不同的问题,那么你可能应该问另一个问题。上面,您的排序算法运行良好(尽管您可能应该添加 {...} 以将所有内容包含在每个 for 循环下方,这样如果您在每个循环下方添加另一个语句,您就不会感到困惑。)换句话说,for (...) for (...) if (...) { ... } 仅适用如果每个循环下面没有其他语句for (...) { for (...) { if (...) {...} } } 无论如何都可以工作。
    • 那应该是size_t *s作为参数。您正在传递一个指针。 (例如,将地址保存为其他值的普通变量)。通过使用size_t s,该函数会收到s 的副本,因此对s 所做的任何更改都不会在调用函数中看到。
    • 非常感谢您的帮助!我会提出一个新问题...
    • 先检查上面的评论——我可能已经回答了:)
    猜你喜欢
    • 1970-01-01
    • 2019-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-26
    • 2013-05-17
    • 1970-01-01
    • 2013-10-09
    相关资源
    最近更新 更多