Implementation of Common sorting methods

Source: Internet
Author: User

# Include <stdio. h>
# Include <limits. h>
# Include <stdlib. h>
# Define size 100
# Deprecision MAX 1000

// Exchange data
Void swap (int * a, int * B)
{
Int temp;
Temp = *;
* A = * B;
* B = temp;
}

Void quicksort (INT data [], int low, int high)
{
Int I, j, key;
If (low {
Key = data [low];
I = low;
J = high;
While (I <j)
{
While (I <j) & (data [J]> = Key) j --;
If (I <j)
Data [I ++] = data [J];
While (I <j) & (data [I] <= Key) I ++;
If (I <j)
Data [j --] = data [I];
}
Data [I] = key;
Quicksort (data, low, I-1 );
Quicksort (data, I + 1, high );
}
}

Void bubblesort (INT data [], int N)
{
Int I, j, temp, tag;
For (I = 0, tag = 1; I <n-1 & tag = 1; I ++)
{
Tag = 0;
For (j = 0; j <n-i-1; j ++)
{
If (data [J]> data [J + 1])
{
Swap (& Data [J], & Data [J + 1]);
Tag = 1;
}
}
}
}

// Select sorting
Void selectionsort (INT data [], int size)
{
Int I, j, min;
// Locate the location of the smallest element from a [I] to a [size-1]
For (I = 0; I <size-1; I ++)
{
Min = I;
For (j = I + 1; j <size; j ++)
If (data [Min]> data [J])
Min = J;
// Exchange data between a [I] And a [Min]
Swap (& Data [I], & Data [Min]);
}
}

// Insert sorting
Void insertsort (INT data [], int size)
{
Int I, j, temp;
For (I = 1; I <size; I ++)
{
Temp = data [I];
J = I-1;
While (j> = 0 & temp <data [J])
Data [J + 1] = data [j --];
Data [J + 1] = temp;
}
}

Struct Node
{
Int min;
Int Max;
};

Void quicksort2 (INT data [], int min, int max)
{
Int I, j, key, top;
Struct node stack [100];

Top =-1;

If (Min <max)
{
Top = 0;
Stack [Top]. min = min;
Stack [Top]. max = max;
}
While (top>-1)
{
// Min max records the left limit and limit of the currently processed range
I = min = stack [Top]. min;
J = max = stack [Top]. Max;
Top --;
Key = data [Min];
While (I <j)
{
While (I <j) & (Key <= data [J]) j --;
If (I <j) data [I ++] = data [J];
While (I <j) & (Key> = data [I]) I ++;
If (I <j) data [j --] = data [I];
} // Process all the items that are smaller than the bound value at one time. Put them on the left and put them on the right if they are larger than the bound value.
Data [I] = key;
If (Min <i-1)
{
Top ++;
Stack [Top]. min = min;
Stack [Top]. max = I-1;
}
If (max> I + 1)
{
Top ++;
Stack [Top]. min = I + 1;
Stack [Top]. max = max;
}
}
}

// Sort by hill
Void shellsort (INT data [], int size)
{
Int * delta, K, I, T, DK, J;
K = size;
Delta = (int *) malloc (sizeof (INT) * (size/2 ));
I = 0;
Do
{
K = K/2;
Printf ("% d", k );
Delta [I ++] = K;
} While (k> 0 );
I = 0;
While (Dk = delta [I])> 0)
{
For (k = delta [I]; k <size; k ++)
{
If (data [k] <data [k-dk])
{
T = data [k];
For (j = k-DK; j> = 0 & T <data [J]; j-= dk)
{
Data [J + dk] = data [J];
}
Data [J + dk] = T;
}
}
I ++;
}
}

// Sorting by count, the best way to rank the number within 100
Void countsort (INT data [], int size)
{
Int temp [Max] = {0 };
Int I, J;
For (I = 0; I <size; I ++)
Temp [DATA [I] ++;
J = 0;
For (I = 0; I <Max; I ++)
{
While (0! = Temp [I])
{
Data [J] = I;
Temp [I] --;
J ++;
}
}
}

// Merge and sort
Void merge (INT data [], int start, int mid, int end)
{
Int temp1 [size], temp2 [size];
Int N1, N2;
Int I, J, K;
N1 = mid-start + 1;
N2 = end-mid;
// Copy the First Half of the array
For (I = 0; I <N1; I ++)
Temp1 [I] = data [start + I];
// Copy the half-end Array
For (I = 0; I <N2; I ++)
Temp2 [I] = data [Mid + I + 1];
// Set the following elements to a large value
Temp1 [N1] = temp2 [n2] = int_max;
I = J = 0;
// Scan the two arrays one by one and place them in the corresponding positions.
For (k = start; k <= end; k ++)
{
If (temp1 [I] <= temp2 [J])
{
Data [k] = temp1 [I];
I ++;
}
Else
{
Data [k] = temp2 [J];
J ++;
}
}
}

Void mergesort (INT data [], int start, int end)
{
Int I;
If (start <End)
{
I = (start + end)/2;
Mergesort (data, start, I );
Mergesort (data, I + 1, end );
Merge (data, start, I, end );
}
}

// Heap sorting
Void heapify (INT data [], int low, int high)
{
Int T, J;
T = data [low];
For (j = 2 * low + 1; j {
If (j <High & Data [J] <data [J + 1])
{
J ++;
}

If (T> data [J])
{
Break;
}

Data [low] = data [J];
Low = J;
}
Data [low] = T;
}

Void buildheap (INT data [], int size)
{
Int I;
For (I = size/2-1; I> = 0; I --)
Heapify (data, I, size-1 );
}

Void heapsort (INT data [], int size)
{
Int I; // lastoforder
Buildheap (data, size );
For (I = size-1; I> 0; I --)
{
Swap (& Data [0], & Data [I]);
Heapify (data, 0, I-1 );
}
}

Int main ()
{
Int I;
Int A [7] = {66,225, 1 };
// Quicksort (A, 0, 6 );
// Bubblesort (A, 7 );
// Selectionsort (A, 7 );
// Insertsort (A, 7 );
// Quicksort2 (A, 0, 6 );
// Shellsort (A, 7 );
// Countsort (A, 7 );
// Heapsort (A, 7 );
// Mergesort (A, 0, 6 );
For (I = 0; I <7; I ++)
Printf ("% d", a [I]);
Return 0;
}

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.