Bubble sort (bubble sort) principle and C language implementation

Source: Internet
Author: User

The bubble sort should be the first lesson for children who are just touching the sort:

(Although this sort is time consuming)

The principle of bubble sort:

We assume this is from small to large (from big to small)

The process of bubbling the sort is simple. First the first record of the keyword and the second record of the keyword is compared, if in reverse order, then two records are exchanged, and then compare the second record and the third record of the keyword. And so on, until the n-1 record and the nth record's keyword are compared.

So, we need to traverse the number of times to n-1, and each time the number of the need to compare the traversal is gradually reduced, and what is the law?

Each time we will the biggest number "sink", so from the back forward is orderly, the first order after the last number is ordered, and so on, and so on, the first order only need to compare to the number of n-i, there are the following:

for (j=0;j<n-i-i;j++) {if ...}

Paste the following code:

1 voidBubble_sort (int*arr,intArr_len)2 {3     inti,j;4     inttemp;5      for(i=0; i<arr_len-1; i++){6          for(j=0; j<arr_len-i-1; j + +){7             if(arr[j]>arr[j+1]){8temp=Arr[j];9arr[j]=arr[j+1];Tenarr[j+1]=temp; One             } A         } -     } -}

This is a regular bubble sort, actually we can do it from both sides.

The first passes from left to right, second through right to left, third again from left to right, and so on:

1# include <stdio.h>2# include <stdlib.h>3# include <string.h>4# include <time.h>5 /*6 void Bubble_sort (int *arr, int arr_len)7 {8 int i;9 int Left=-1,right=arr_len;Ten While (left<right) { One For (i=left+1;i<right-1;i++) { A if (arr[i]>arr[i+1]) { - int temp=arr[i]; - arr[i]=arr[i+1]; the arr[i+1]=temp; -             } -         } - right--; + For (i=right-1;i>left+1;i--) { - if (Arr[i]<arr[i-1]) { + int temp=arr[i]; A Arr[i]=arr[i-1]; at arr[i-1]=temp; -             } -         } - left++; -     } - } in */ - voidBubble_sort (int*arr,intArr_len) to { +     intleft=-1, right=Arr_len; -     inti; the     inttemp; *      while(left<Right ) { $i=left+1;Panax Notoginseng          while(left<right&&i<right-1){ -             if(arr[i]>arr[i+1]){ thetemp=Arr[i]; +arr[i]=arr[i+1]; Aarr[i+1]=temp; the             } +i++; -         } $right--; $i=right-1; -          while(left<right&&i>left+1){ -             if(arr[i]<arr[i-1]){ thetemp=Arr[i]; -arr[i]=arr[i-1];Wuyiarr[i-1]=temp; the             } -i--; Wu         } -left++; About     } $ } -  - voidBubble_sort_normal (int*arr,intArr_len) - { A     inti,j; +     inttemp; the      for(i=0; i<arr_len-1; i++){ -          for(j=0; j<arr_len-i-1; j + +){ $             if(arr[j]>arr[j+1]){ thetemp=Arr[j]; thearr[j]=arr[j+1]; thearr[j+1]=temp; the             } -         } in     } the } the  About intMainvoid) the { the     DoubleGetTime; the clock_t start,finish; + Srand (Time (NULL)); -     intArr_len; the     int*arr;Bayi     int*Arr_normal; the      while(printf ("Give me the length of array.\n"), scanf ("%d", &arr_len) = =1){ the         if((null== (arr= (int*) malloc (sizeof(int)*Arr_len))))  { -printf"Malloc error!! \ n"); -         } the         if((null== (arr_normal= (int*) malloc (sizeof(int)*Arr_len))))  { theprintf"Malloc to Arr_normal error\n"); the         } the         inti; -          for(i=0; i<arr_len;i++){ theArr_normal[i]=arr[i]=rand ()% -; the         } thestart=clock ();94         /* the printf ("Show TIME!!!!! \ n "); the For (i=0;i<arr_len;i++) { the printf ("%d\t", Arr[i]);98         } About printf ("\ n"); -         */101 Bubble_sort (arr, arr_len);102         /*103 For (i=0;i<arr_len;i++) {104 printf ("%d\t", Arr[i]); the         }106 printf ("\ n");107         */108Finish=clock ();109Gettime= (Double) (Finish-start)/clocks_per_sec; theprintf"The time is %fs\n", getTime);111start=clock (); the Bubble_sort_normal (Arr_normal, Arr_len);113Finish=clock (); theGettime= (Double) (Finish-start)/clocks_per_sec; theprintf"The normal time is %fs\n", getTime); theprintf"Once again? (y/n): \ n");117         Charch;118scanf"%*c%c",&ch);119         if(ch=='y'&&ch!='N'){ -             Continue;121         }122          Break;123     }124     return 0;

Time comparison of two methods: Machine (acer4752g i5-2430) system: Debian wheezy compiler: GCC

Give me the length of array.
100000
The time is 35.520000s
The normal time is 44.210000s
Once again? (y/n):

The second method is the same order of magnitude, although the method is the same because the number of comparisons is less than the first time.

Bubble sort (bubble sort) principle and C language implementation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.