linux - Bubble Sort Time C program -


the test create array of size 10000. initialize values 10000 down 1 , use bubble sort reverse order.as bubble sort 1 of worst possible sorts, should take long time. however, resolution of timing limited.

the solution put task inside loop repeats thousand or million times—whatever takes numbers can deal with. getting errors in code. please see code below.

the following commands run on linux:
gcc -o sort sort.c -o2
time ./sort

gcc -m32 -o sort sort.c -o2 -march=pentium4
time ./sort

#include <stdio.h>  void bubblesort(int numbers[], int array_size) {      int i, j, temp;      (i =0; <array_size; i++)     {         (j =0; j<array_size-1; j++)         {             if (numbers[j] > numbers[j+1])  {                 temp = numbers[j];                 numbers[j] = numbers[j+1];                 numbers[j+1] = temp;             }         }     } }  int main(void) {     int array[10000];     int i;      for(i=10000;i!=0;i--)     {         array[i-1]=i;     }     bubblesort(array,10000);     for(i=0;i<10000;i++)     {         printf("%d\n",array[i]);     }     return 0; } 

the following code stores numbers in array in ascending order.

    for(i=10000;i!=0;i--)     {         array[i-1]=i;     } 

your sorting algorithm sorts numbers in ascending order, won't find change numbers in array.

        if (numbers[j] > numbers[j+1])           {             temp = numbers[j];             numbers[j] = numbers[j+1];             numbers[j+1] = temp;         } 

change above if follows,

        if (numbers[j] < numbers[j+1])          {             temp = numbers[j];             numbers[j] = numbers[j+1];             numbers[j+1] = temp;         } 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -