# Analysis of seven sorting algorithms and implementation of C language source code

Keywords: shell less

# Analysis of seven sorting algorithms and implementation of C source code

## Selection sort

First of all, initialize the first element of the array, the outer loop traverses the sorted array in turn, the inner loop traverses the array again, until the end of the array. If the elements need to be exchanged, exchange them. When the double loop traversal ends, the sorted array is obtained.
Time complexity: O(n2)
Worst case: O(n2)
Best case: O(n2)
Stability: stable
Applicable to small n

```#include <stdio.h>
#define MAX 50000

int n, a[MAX], t;

int main()
{
scanf("%d", &n);
for(int i=1; i<=n; i++)
scanf("%d", &a[i]);
for(int i=1; i<n; i++)
{
for(int j=i+1; j<=n; j++)
{
if (a[i] > a[j]) {
t = a[i];
a[i] = a[j];
a[j] = t;
}
}
}
for(int i=1; i<=n; i++)
printf("%d ", a[i]);
return 0;
}

```

## Bubble sort

Compare two adjacent elements in turn. If the former element is larger than the latter, exchange it until the last element is the largest. Then repeat the same operation from the first element again until the next to last element is the next largest element. At the end of the cycle, get the sorted set.
Time complexity: O(n2)
Worst case: O(n2)
Best case: O(n)
Stability: stable
Applicable to small n

```#include <stdio.h>
#define MAX 1000001

int a[MAX], n, t;

int main()
{
scanf("%d", &n);
for(int i=0; i<n; i++)
scanf("%d", &a[i]);
for(int i=0; i<n; i++)
{
for(int j=0; j<n-i-1; j++)
{
if(a[j] > a[j+1]) {
t=a[j]; a[j]=a[j+1]; a[j+1]=t;
}
}
}
for(int i=0; i<n; i++)
printf("%d ", a[i]);
return 0;
}

```

## Insertion sort

The front part of the sequence is in order. The elements of the latter sequence are inserted into the front sequence in turn. There is only one element in the initial state sequence, that is, the first element. In the process of inserting the elements of the disordered sequence into the ordered sequence, traversing the ordered sequence in reverse order is adopted, which will be a little more tedious than traversing the sequence, but the efficiency will be higher when the sequence itself is near the ordered state.
Time complexity: O(n2)
Worst case: O(n2)
Best case: O(n)
Stability: stable
Applicable to small n

```#include <stdio.h>
#define MAX 100001

int n, a[MAX], t, j;

int main()
{
scanf("%d", &n);
for(int i=1; i<=n; i++)
scanf("%d", &a[i]);
for(int i=1; i<=n; i++)
{
t = a[i];
for(j=i-1; j>=1; j--)
{
if (t < a[j]) {
a[j+1] = a[j];
}
else break;
}
a[j+1] = t;
}
for(int i=1; i<=n; i++)
printf("%d ", a[i]);
return 0;
}

```

## Shell Sort

Insert an improved version of sorting. In order to reduce the number of data moves, when the initial sequence is large, a larger step is taken, usually half the length of the sequence. At this time, only two elements are compared and exchanged once. After that, the step is cut in half until the step is 1, which is the insertion sort. Because the sequence is close to the order at this time, the number of data moves when the elements are inserted will be relatively small, and the efficiency will be improved.
Time complexity: O(n3/2)
Worst case: O(n2)
Best case: O(n)
Stability: unstable

```#include <stdio.h>
#define MAX 500000

int n, a[MAX], t;

int main()
{
int t, k;
scanf("%d", &n);
for(int i=0; i<n; i++)
scanf("%d", &a[i]);
for(int i=n/2; i>0; i/=2)
{
for(int j=i; j<n; j++)
{
k = j;
t = a[j];
if (a[k] < a[k-i]) {
while (k-i>=0&&t < a[k-i]) {
a[k] = a[k-i];
k -= i;
}
a[k] = t;
}
}
}
for(int i=0; i<n; i++)
printf("%d ", a[i]);
return 0;
}

```

## Bucket sort (cardinal sort)

Bucket sorting is not a sort algorithm based on comparison, the core of which is to convert the input data values into keys and store them in the extra array space (bucket). As a sort of linear time complexity, bucket sort requires that the input data must be an integer with a certain range.
Time complexity: O(d(r+n))
Worst case: O(d(r+n))
Best case: O(d(r+rd))
Stability: stable

```#include <stdio.h>
#define MAX 100000

int a[MAX]={0}, n, t;

int main()
{
scanf("%d", &n);
for(int i=1; i<=n; i++)
{
scanf("%d", &t);
a[t]++;
}
for(int i=0; i<=10000; i++)
for(int j=1; j<=a[i]; j++)
printf("%d ", i);
return 0;
}

```

## Quick sort

Select a datum element, place the value less than the datum element on its left side in turn, and the value greater than or equal to the datum element on its right side; then, recursively operate the same set on its left and right sides, fast sorting is the most widely used algorithm.
Time complexity: O(nlog2n)
Worst case: O(n2)
Best case: O(nlog2n)
Stability: unstable
Applicable to the case of large n

```#include <stdio.h>

int n, a[100001], k;

void qs(int l, int r)
{
int i, j, t, temp;
if (l > r) return ;

temp = a[l];
i = l;
j = r;
while (i != j)
{
while(a[j] <= temp && i<j)
j--;
while(a[i] >= temp && i<j)
i++;
if (i < j) {
t = a[i];
a[i] = a[j];
a[j] = t;
}
}
a[l] = a[i];
a[i] = temp;
qs(l, i-1);
qs(i+1, r);
}

int main()
{
int i, j, t;
scanf("%d", &n);
for( i=1; i<=n; i++)
scanf("%d", &a[i]);
qs(1, n);
for( i=1; i<=n; i++)
printf("%d ", a[i]);
return 0;
}

```

## Heap sort

The idea of heap sorting is realized by the largest heap in binary heap. First, the sequence to be sorted is abstracted as a binary tree, and the maximum heap (or the minimum heap) is constructed; then, the maximum element (i.e. the root node element) is exchanged with the last element of the sequence to be sorted (i.e. the leaf node element at the deepest right of the binary tree); each time, the position of the last element is refreshed (minus 1) until it intersects the first element, i.e Finish sorting.
Time complexity: O(nlog2n)
Worst case: O(nlog2n)
Best case: O(nlog2n)
Stability: unstable
Applicable to the case of large n

```#include <stdio.h>
#define MAX 500000

int s[MAX], n, num, k;

void swap(int x, int y)
{
int t;
t = s[x]; s[x] = s[y]; s[y] = t;
}

void shiftdown(int x)
{
int t, book = 0;
while (x*2 <= n&&book == 0) {
if (s[x] > s[x*2]) {
t = x*2;
}
else t = x;
if (x*2+1 <= n) {
if (s[x*2+1] < s[t]) {
t = x*2+1;
}
}
if (x != t) {
swap(x, t);
x = t;
}
else book = 1;
}
}

void create()
{
for(int i=n/2; i>=1; i--)
{
shiftdown(i);
}
}

void heapsort()
{
while(n > 0) {
swap(1, n);
n--;
shiftdown(1);
}
}

int main()
{
scanf("%d", &num);
n = num;
for(int i=1; i<=n; i++)
scanf("%d", &s[i]);
create();
heapsort();
for(int i=num; i>0; i--)
printf("%d ", s[i]);
return 0;
}

```

Thank you for your reference Blog links

Published 1 original article, praised 0 and visited 5

Posted by alex_funky_dj on Thu, 05 Mar 2020 20:11:39 -0800