The common sort method of C language--bubbling method, selection method, inserting method

Source: Internet
Author: User

When we want to sort a set of data in C is a common sort method with bubbling method, selection method , insertion method

Bubble Sort Method (ascending): Suppose a set of data a[0], a[1], a[2], A[3]...a[n], first round : compare A[0] with a[1], a[1] and a[2]...a[i] and a[i+1]...a[n-1] The size of a[n], if a[i] and a[i+1] are not ascending (ie A[i] > a[i+1]), then the values of a[i] and a[i+1 are exchanged, and A[n] is the maximum after the first round; the second round : Using the same method of the first round, compare a[0] With A[1], a[1] and a[2]...a[i] and a[i+1]...a[n-2] and A[n-1],a[n-1] is the largest of the remaining, and so on, after the n-1 wheel, the order of the array is in ascending sequence

1 intBubblesort (intArray[],intN) {2      intTemp//Intermediate Variables3      4       for(inti =0; I < n-1; i++){5           for(intj =0; J <9-i;j++){6              //if the adjacent two elements are not ascending, the values of the two elements are exchanged7              if(Array[j] > array[j +1]) {8temp =Array[j];9ARRAY[J] = array[j +1];TenArray[j +1] =temp; One              } A          } -      } -       the}

Bubbling Method (an array of 5 elements):

Select sorting (Ascending): Suppose a set of data a[0], a[1], a[2], A[3]...a[n], the first round: the value of a[0] and each subsequent number of comparisons, if a[0] large, interchange two number of values, otherwise not move, after the first round, a[0] The value is the smallest in the array, and the second round: a[1] is the same as the first round of each number following it, after which the value of a[1] is the smallest remaining value, and so on, after the n-1 wheel, the order of the array is sorted by ascending

1 voidSelectsort (intArray[],intN) {2      inttemp;3      4       for(inti =0; I < n-1; i++) {5           for(intj = i +1; J <n; J + +) {6          7              if(Array[i] >Array[j]) {8temp =Array[j];9ARRAY[J] =Array[i];TenArray[i] =temp; One   A              } -          } -      } the}

In the above code because it is encountered Array[i] > array[j], the exchange of the values of the two, so that once encountering a large amount of data, it will reduce the efficiency of the sorting, in each round I can record the minimum number of small mark and finally and A[i] interchange, which will greatly improve the efficiency of the sorting, The code is as follows:

1 voidSelectsort (intArray[],intN) {2     intmin,temp;3         4      for(inti =0; I < n-1; i++) {5min = i;//assuming that the element value of subscript i is minimal6          for(intj = i +1; J <n; J + +) {7             //The Loop assigns the subscript to min the element with the smaller value than the array[min]8             if(Array[min] >Array[j]) {9Min =J;Ten                      One             } A         } -         //the minimum number of array[min] and A[i] swaps after the end of the inner loop -         if(I! =min) { thetemp =Array[min]; -Array[min] =Array[i]; -Array[i] =temp; -         } +     } -}

Selection Method : (an array of 5 elements)

Insert Sort Method (Ascending): Also called direct insert sort, the idea of this sort is to place the elements of an unordered array into the correct position in an ordered array, one after the other. Suppose a set of data a[0], a[1], a[2], A[3]...a[n], the first round: the a[0] as an ordered unit of Prime Group, the a[1] into the appropriate position, so a[0], a[1] is a new ordered array; second round: Put a[2] in an orderly array of appropriate positions , so a[0], a[1], a[2] is the new ordered array, and so on, and finally through the n-1 wheel Insert, the order of the array is in ascending sequence

1 intInsertsort (intArray[],intN) {2      for(inti =1; i < n;i++)//external loops, n-1 times from the second number to the last number3     {4temp = Array[i];//the number that needs to be inserted assigns a value to the variable temp5          for(intj = I1; J >=0&&t < ARRAY[J]; j--)//Inner Loop, which compares the number of inserts you want to insert with the number preceding it6Array[j +1] = Array[j];//Entering a loop indicates that the current value is larger than the number of insertions, and the current value is moved backward by one7array[j+1] = temp;//Jumping out of the inner loop indicates that the current value is smaller than the number inserted, at which point the number to be inserted is assigned to the previous number8         9     }Ten}

Insert Method (Ascending):

Note: The advantages and disadvantages of different C sorting methods and other methods can read the C language Chinese network or reference Baidu Library address is:

Http://wenku.baidu.com/view/c3054c0f7cd184254b353516.html

The common sort method of C language--bubbling method, selection method, inserting method

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.