【问题标题】:bubble sorting of Array in cc中数组的冒泡排序
【发布时间】:2021-04-10 18:52:34
【问题描述】:

我对数组做了选择和冒泡排序,选择排序没问题,但是冒泡排序从数组的第(5)个元素开始排序,从{0到4}不排序

#include <stdio.h>
#include <stdlib.h>

////  Makeing a swaping Function/////
void swap(int* x,int* y)
{
    int temp= *y;
    *y= *x;
    *x= temp;
}

////  Makeing a sorting  Function  selection_ sort/////
void selection_sort_elements(int array[],int len)
{
    int i,j;
    int flag =1;
 for (i=0;i<len;i++)
 {
     printf("Number of Iteration: %d \n",i);
     for(j=i+1;j<len;j++)
     {

         if(array[i]>array[j])
         {
             swap(&array[i],&array[j]);
         }
     }
 }

}
////  Makeing a sorting  Function  Bubble sort/////
void bubble_sort_elements(int array[],int len)
{
    int i=0,j=0;
    //int flag =1;
 for (i = 0; i < len ;i++)
 {
     printf("Number of Iteration: %d \n",i);

     //if(flag == 0){
         //   return;
         //}
         //flag=0;
     for(j = i+1 ; j < len ;j++)
     {
         if(array[j] < array[j-1])
         {
            // flag=1;
             swap(&array[j],&array[j-1]);
         }
     }
 }

}

int main()
{
    int arr[10];
    int i=0;
    printf("Please Enter A Five Element Of The Array\n");
    for (i=0;i<10;i++)
        {
        scanf("%d",&arr[i]);
        }

    printf(" You Entered The Array:");
    for (i=0;i<10;i++)
        {
        printf(" %d ", arr[i]);
        }

    bubble_sort_elements(arr,sizeof(arr)/sizeof(arr[0]));      // Calling Sorting Function

    printf("\n Now We Sorted Your Array :");
    for (i=0;i<sizeof(arr)/sizeof(arr[0]);i++)
        {
        printf(" %d ", arr[i]);
        }

    //Getting The max Number in the array and its position
    printf("\n The MAX Number In The Array IS %d And it location is %d ", arr[(sizeof(arr)/sizeof(arr[0]))-1], sizeof(arr)/sizeof(arr[0])-1);
    //Getting The Min Number in the array and its position
    printf("\n The Min Number In The Array IS %d And it location is %d ", arr[0], sizeof(arr)/sizeof(arr[0])-sizeof(arr)/sizeof(arr[0]));

    return 0;
}

我对数组做了选择和冒泡排序,选择排序没问题,但是冒泡排序从数组的第(5)个元素开始排序,从{0到4}没有排序 我为回复上传了图片

【问题讨论】:

  • GCC 作为gcc -Wall -Wextra -g 调用来编译你的代码,改进你的代码以获得没有警告(见this C reference...)然后使用GDB 调试器来了解哪里出了问题在你的程序中。如果允许,请从现有的开源程序(如GNU bashgithub.com 上的许多其他程序...)中获取灵感。另请阅读bubble sort 上的维基百科和您的 C 编译器的文档

标签: arrays c sorting codeblocks


【解决方案1】:

您的冒泡排序算法是错误的。您要么需要不断降低天花板的上升扫掠,要么需要不断提升地板的下降扫掠。你有一个不断前进的地板的上升扫描。如果您想保持上升扫描,您的内部循环应该是for (j = 1; j&lt;(len-i); ++j),但要修复算法以使用不断减小的上限。

void bubble_sort_elements(int array[], size_t len)
{
    size_t i = 0, j = 0;

    for (i = 0; i < len; i++)
    {
        printf("Number of Iteration: %zu \n", i);

        for (j = 1; j<(len-i); ++j)
        {
            if (array[j] < array[j - 1])
            {
                // flag=1;
                swap(&array[j], &array[j - 1]);
            }
        }
    }
}

或者,您可以放弃 i 并在外循环上不断减少 len

void bubble_sort_elements(int array[], size_t len)
{
    while (len-- > 0)
    {
        for (size_t j = 0; j<len; ++j)
        {
            if (array[j+1] < array[j])
                swap(array+j, array+j+1);
        }
    }
}

最后,如果您希望交换检测在已排序的序列到位后将其短路,则后一种算法可以立即适应:

void bubble_sort_elements(int array[], size_t len)
{
    int swapped = 1;
    while (len-- > 0 && swapped)
    {
        swapped = 0;
        for (size_t j = 0; j<len; ++j)
        {
            if (array[j+1] < array[j])
            {
                swap(array+j, array+j+1);
                swapped = 1;
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 2015-01-27
    • 2016-02-10
    • 1970-01-01
    • 2013-09-28
    • 2020-09-13
    • 2015-04-10
    • 2014-03-26
    • 2018-11-13
    相关资源
    最近更新 更多