Comparison of Bubble sorting, insertion sorting, and selection sorting algorithms in C Language

Source: Internet
Author: User

Mastering common sorting algorithms can save a lot of time in actual project development. There are differences in the execution efficiency of each sort algorithm. These tiny time differences may not be felt in common connections, however, it is particularly important to involve a large amount of data or systems with tight resources, such as embedded systems. The following describes three common sorting algorithms and their comparison of execution efficiency.

Bubble Sorting:

Train of Thought: compare two adjacent numbers, and set a smaller number to the front. If there are n numbers, we need to perform n-1 comparison. In the first comparison, we need to perform n-1 two-to-one comparison, in the J-train comparison, we need to perform the n-J comparison.

Void bubllesort (INT arr [], int count) {int I, j, temp; For (j = 0; j <count-1; j) /* Bubble Method to sort n-1 times */for (I = 0; I <count-j-1; I ++)/* after the elements with relatively large values sink, just sink the maximum values of the remaining elements. */{If (ARR [I]> arr [I 1]) /* sink a large element to the end */{temp = arr [I 1]; arr [I 1] = arr [I]; arr [I] = temp ;}}}

Insert sorting:

Idea: after obtaining the array to be sorted, the array is divided into two parts. The first element of the array is a part, and the remaining element is a part, and then starts from the second element of the array, compared with all the elements before the element, if the previous element is not greater than the element, the position of the element remains unchanged. If the value of an element is greater than that of the element, then, record the location of the element in love. For example, if I, the position of the element is K, it moves one element from the position I to K, then, move the value at the K position to the I position. In this way, the location of K is found. Each element is executed in this way, and an ordered array is finally obtained.

Implementation Code:

Void insertsort (INT arr [], int count) {int I, j, temp; for (I = 1; I <count; I ++) // The array is divided into two parts, starting from the second array element {temp = arr [I]; // operate the current element, First saved in other variables for (j = I-1; j>-1 & arr [J]> temp; j --) // locate the appropriate position from the previous element of the current element, keep searching for the first element {arr [I] = arr [J]; arr [J] = temp ;}}}

Select sort:

Ideas:

First, use an element as the benchmark and start scanning from one direction, for example, scanning from left to right, with a [0] as the benchmark, and then from a [0]... Find the smallest element in a [9] and exchange it with a [0. Then shift the reference position one to the right and repeat the above action. For example, use a [1] as the reference to find a [1] ~ The smallest of a [9], which is exchanged with a [1. The sorting ends until the base position is moved to the last element of the array.

Implementation Code:

Void selectsort (INT arr [], int count) {int I, j, Min, temp; for (I = 0; I <count; I ++) {min = arr [I]; // This element is used as the reference for (j = I 1; j <count; j) // data from J is arranged, so start from J and find the smallest {If (min> arr [J]) among the remaining elements. // put the smallest of the remaining elements in arr [J] {temp = arr [J]; arr [J] = min; min = temp ;}}}}

Efficiency Comparison:

To make it more visible, each sorting algorithm is executed 10000 times. The main function of the test program is as follows:

# Include <stdio. h> # include <stdlib. h> # include <sys/time. h> # include <unistd. h> # define Max 6 int array [Max]; int COUNT = max;/******** create an array, and enter the element ***********/void buildarray () {int A, I = 0; printf ("Enter the array element :"); for (; I <count; I) {scanf ("% d", & A); array [I] = A;} printf ("\ n ");} /********************/void traverse (INT arr [], int count) {int I; printf ("array output:"); for (I = 0; I <count; I ++) printf ("% d \ t ", arr [I]); printf ("\ n");} void bubllesort (INT arr [], int count) {int I, j, temp; For (j = 0; j <count-1; j ++)/* Bubble Method to sort n-1 times */for (I = 0; I <count-j-1; I ++) /* after a large element is sunk, only the maximum value of the remaining element is sunk. */{If (ARR [I]> arr [I + 1]) /* sink a large element to the end */{temp = arr [I + 1]; arr [I + 1] = arr [I]; arr [I] = temp ;}} void insertsort (INT arr [], int count) {int I, j, temp; for (I = 1; I <count; I ++) // The array is divided into two parts, starting from the second array element {temp = arr [I]; // you can operate on the current element, for (j = I-1; j>-1 & arr [J]> temp; j --) // start from the previous element of the current element to find the appropriate position and continue to find the first element {arr [I] = arr [J]; arr [J] = temp ;}} void selectsort (INT arr [], int count) {int I, j, Min, temp; for (I = 0; I <count; I ++) {min = arr [I]; // reference this element for (j = I 1; j <count; j ++) // data from the forward direction of J is arranged, so from J, find the smallest {If (min> arr [J]) of the remaining elements. // put the smallest of the remaining elements in arr [J] {temp = arr [J]; arr [J] = min; min = temp ;}}}} int main () {int I; struct timeval TV1, TV2; struct timezone TZ; buildarray (); // create an array traverse (array, count ); // output the initial array gettimeofday (& TV1, & Tz); for (I = 0; I <10000; I ++) bubllesort (array, count ); // bubble sort gettimeofday (& TV2, & Tz); printf ("% d: % d/N", tv2. TV _ sec-tv1. TV _sec, tv2. TV _ usec-tv1. TV _usec); traverse (array, count); // output the sorted array gettimeofday (& TV1, & Tz); for (I = 0; I <10000; I ++) insertsort (array, count ); // Insert the sort gettimeofday (& TV2, & Tz); printf ("% d: % d/N", tv2. TV _ sec-tv1. TV _sec, tv2. TV _ usec-tv1. TV _usec); traverse (array, count); // output the sorted array gettimeofday (& TV1, & Tz); for (I = 0; I <10000; I ++) selectsort (array, count ); // Insert the sort gettimeofday (& TV2, & Tz); printf ("% d: % d/N", tv2. TV _ sec-tv1. TV _sec, tv2. TV _ usec-tv1. TV _usec); traverse (array, count); // output the sorted array return 0 ;}

Compile: gcc-g-wall sort_test.c-O sort_test

Run:./sort_test

The result is as follows:

Through multiple tests, insert sorting is the fastest.

Article transferred from: http://blog.chinaunix.net/uid-26758209-id-3139356.html

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.