Various sorting algorithm codes (c language version)

Source: Internet
Author: User

Select sort

#include <stdio.h>/** Select sort * Stability: unstable * time complexity: O (n^2) **/voidSelect_sort (intA[],intLintR) {     for(intM_v, M_idx, t, i = l; I < R; ++i) {m_v= A[i]; M_idx =i;  for(intj = i +1; J < R; ++j) {if(M_v >A[j]) {M_v=A[j]; M_idx=J; }} t= A[i]; A[i] = A[m_idx]; A[M_IDX] =T; }}intMainvoid){    inta[ -]; intN scanf"%d", &N);  for(inti =0; I < n; ++i) scanf ("%d", &A[i]); Select_sort (A,0, N);  for(inti =0; I < n; ++i) printf ("%d", A[i]); return 0;}

Bubble sort

#include <stdio.h>/** Bubble Sort * Stability: Stable * Time complexity: O (n^2) **/voidBubble_sort (intA[],intLintR) {     for(inti = l; I < R; ++i) { for(intj = l; J < R-i-1; ++j) {if(A[j] > a[j +1]) {                intTMP =A[j]; A[J]= A[j +1]; A[j+1] =tmp; }        }    }}intMainvoid){    inta[ -]; intN scanf"%d", &N);  for(inti =0; I < n; ++i) scanf ("%d", &A[i]); Bubble_sort (A,0, N);  for(inti =0; I < n; ++i) printf ("%d", A[i]); return 0;}

Insert Sort

#include <stdio.h>/** Insert Sort * Stability: Stable * Time complexity: O (n^2) **/voidInsert_sort (intA[],intLintR) {     for(intTMP, J, i = L +1; I < R; ++i) {tmp= A[i], j = i-1;  while(J >= L && tmp < A[J]) a[j+1] = a[j--]; A[j+1] =tmp; }}intMainvoid){    inta[ -]; intN scanf"%d", &N);  for(inti =0; I < n; ++i) scanf ("%d", &A[i]); Insert_sort (A,0, N);  for(inti =0; I < n; ++i) printf ("%d", A[i]); return 0;}

Hill sort

#include <stdio.h>/** Hill Sort * Stability: unstable * time complexity: O (N*LOGN) **/voidShell_insert_sort (intA[],intLintRintd) {     for(intTMP, J, i = L + D; I < R; ++i) {tmp= A[i], j = i-D;  while(J >= L && tmp <A[j]) {A[j+ d] =A[j]; J-=D; } a[j+ d] =tmp; }}voidShell_sort (intA[],intLintR) {    intD = (r-l)/2;  while(d >=1) {Shell_insert_sort (A, L, R, D); D/=2; }}intMainvoid){    inta[ -]; intN scanf"%d", &N);  for(inti =0; I < n; ++i) scanf ("%d", &A[i]); Shell_sort (A,0, N);  for(inti =0; I < n; ++i) printf ("%d", A[i]); return 0;}

Heap Sort

#include <stdio.h>/** Heap Sort * Stability: unstable * time complexity: O (N*LOGN) **///Big Top PilevoidHeap_adjust (intA[],intFaintN) {    intcd = FA *2+1; intTMP =A[FA];  while(CD <N) {if(CD +1< n && A[CD] < A[CD +1]) cd++; if(A[fa] >= A[CD]) Break; A[FA]=A[CD]; FA=cd; CD= FA *2+1; A[FA]=tmp; }}voidBuild_heap (intA[],intN) {    //Ignore Leap node     for(inti = (N-1) /2; I >=0; --i) {heap_adjust (A, I, N); }}voidHeap_sort (intA[],intLintR) {Build_heap (a+ L, R-l);  for(intTMP, I = R-1; i > L; --i) {tmp= A[i]; A[i] = a[0]; a[0] =tmp; Heap_adjust (A+ L,0, i); }}intMainvoid){    inta[ -]; intN scanf"%d", &N);  for(inti =0; I < n; ++i) scanf ("%d", &A[i]); Heap_sort (A,0, N);  for(inti =0; I < n; ++i) printf ("%d", A[i]); return 0;}

Various sorting algorithm codes (c language version)

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.