C language Implementation Select the sort, direct insert sort, bubble sort sample _c language

Source: Internet
Author: User

Select sort
selection sorting is a simple and intuitive sort algorithm whose core idea is to traverse an array, find the smallest element in a sequence that has never been sorted, and place it at the end of the sorted sequence.

Complexity of Time: O (n^2)

Stability: Unstable

 
 * * @brief  selection Sort
 *
 /void Selection_sort (int a[], int n)
 {
   int i, J, Min, tmp;
   
   for (i = 0; i < n-1; ++i) {
     min = i;
     for (j = i+1 J < n; ++j) {
       if (A[j] < a[min]) {
         min = j;
       }
     }
     if (min!= i) {
       tmp = a[min];
       A[min] = A[i];
       A[i] = tmp;}}}
 


Direct Insert Sort
Direct insertion Ordering is a relatively easy to understand sort algorithm whose core idea is to traverse an array and insert elements of the array into the sorted sequence.

Complexity of Time: O (n^2)

Stability: Stability

Realize:

 /* @brief insetion Sort
 * Insert the new element
 to the sorted subarray
 /void Insertion_sort (int a[], in T n)
 {
   int i, j, num;
 
   for (i = 1; i < n; ++i) {
     num = a[i];
     for (j = i-1 J >= 0 && a[j] > num;--j)
       a[j+1] = a[j];
     A[J+1] = num;
   }
 }


Bubble Sort
Bubble Sort is one of the most basic sorting algorithms, whose core idea is to traverse the array from the back forward, comparing a[i] and a[i-1], if a[i] is smaller than a[i-1, then exchange the two. After such a traversal, the smallest element is at the top of the array, followed by the traversal of the child array except for the smallest element. N-Time (n-array elements) after the traversal is arranged. The outer loop is n times, the inner layer cycle is n-1, n-2 ... 1 times.

Complexity of Time: O (n^2)

Stability: Stability

Realize:

 /* @brief  bubble sort * move "
 smallest element to" front in every single loop/
 void
 bubble_sor T (int a[], int n)
 {
   int i, J, TMP;
 
   for (i = 0; i < n; ++i) {for
     (j = n-1; j > i;--j) {
       if (A[j] < a[j-1]) {
         tmp = a[j];
         A[J] = a[j-1];
         A[J-1] = tmp;}}}
 

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.