Common sort C implementation

Source: Internet
Author: User

Bubble

int* maopao (int a[], int n)

{

int temp = 0;

for (int i = 0; i<n-1; i++) {

for (int j = 0; j<n-1-i; j + +) {

if (A[j] > a[j+1]) {

temp = A[j];

A[J] = a[j+1];

A[J+1] = A[j];

}

}

}

return A;

}

Insert Sort

int* Insert (int a[],int N)

{

Divides an array into two segments, the former is the new queue after insertion, and the back segment is the queue to be inserted

for (int i = 1; i<n; i++) {

Value to insert

int temp = A[i];

Find insertion Position

int j = i-1;

for (; j>0; j--) {

if (A[j] > Temp) {

A[J+1] = A[j];

}

}

A[J] = temp;

}

return A;

}

Binary Insert Sort

That is, when looking for a location, because with the insertion queue is already sorted, you can use binary search to quickly locate the position to be inserted

int *twoinsert (int a[], int n)

{

Divides an array into two segments, the former is the new queue after insertion, and the back segment is the queue to be inserted

for (int i = 1; i<n; i++) {

Value to insert

int temp = A[i];

Find insertion Position

int k = i-1;//The dividing line between sorted and unordered queues

Binary positioning

int low = 0;

int high = k;

int middle = 0;

while (Low < high) {

Middle = (high + low)/2;

if (A[middle] > Temp) {

Go ahead.

High = MAX (middle-1, 0);

}else if (A[middle] <= temp) {

Look backwards.

Low = MIN (middle + 1, k);

}

}

Determine location

int position = low;

if (A[low] > Temp) {

Position = low;

}else {

Position = low + 1;

}

Move back

int j = k+1;

while (J > position) {

A[J] = a[j-1];

j--;

}

Insert

A[position] = temp;

}

return A;

}

Two-point Search

int twosearch (int a[], int n, int value)

{

int low = 0;

int hight = n-1;

int middle = 0;

while (low <= hight) {

Middle = (hight + low)/2;

if (A[middle] > value) {

Go ahead.

Hight = middle-1;

}else if (A[middle] < value) {

Look backwards.

Low = middle + 1;

}else {

return middle;

}

}

return-1;

}

Hill sort, insert sort based on the transformation, grouped by component D, gradually reduce d component until d==1, sort end

Because of the grouping reason, Hill sort is unstable

int *shellsort (int a[], int n)

{

Component D Descending Group increment

for (int d = N/5; d>0; d--) {

Traverse all groups, group 0-d

for (int j = 0; J < D; j + +) {

Sorting within groups

for (int i = j; i + D < n; i + = d) {

Inserting j+1 elements into an ordered group of 0-J

int k = i; The last one to insert sequence

int temp = a[k + d]; The element being inserted

while (k >= 0) {

if (A[k] > Temp) {

A[k + d] = a[k];

K = k-d;

}else {

Break

}

}

A[K+D] = temp;

}

}

}

return A;

}

Quick Sort

void Kuaisu (int a[], int n)

{

if (n <= 1) {

Return

}

int low = 0;

int high = n-1;

int t = low;

int temp = a[t];

while (Low < high) {

while (A[high] >= a[t] && high > Low) {

High--;

}

if (A[high] < a[t]) {

A[T] = A[high];

A[high] = temp;

t = high;

}

while (A[low] <= a[t] && Low < high) {

Low + +;

}

if (A[low] > A[t]) {

A[T] = A[low];

A[low] = temp;

t = low;

}

if (low = = high) {

Kuaisu (&a[0], low);

Kuaisu (&a[high+1], n-high-1);

}

}

return;

}

void swap (int a[],int p0,int p1)

{

int temp = a[p0];

A[P0] = A[p1];

A[P1] = temp;

}

#pragma mark-Sort by heap

void balance (int a[],int len,int root/*1-n*/)

{

if (Root < 1) {

NSLog (@ "balance wrong root");

Return

}

int right = 2 * root + 1;

int left = 2 * root;

root-= 1;

Right-= 1;

Left-= 1;

if (Len > right) {

int big = right;

if (A[right] <= A[left]) {

Big = left;

}

if (A[root] < A[big]) {

Swap (A, root, big);

Balance (A, Len, big + 1);

}

}else if (len > left) {

if (A[root] < A[left]) {

Swap (A, root, left);

Balance (A, Len, left + 1);

}

}

}

void buildheap (int a[],int len)

{

for (int i = LEN/2; i > 0; i--) {

Balance (A, Len, i);

}

}

void heapsort (int a[], int len)

{

Buildheap (A, Len);

while (Len > 0) {

Swap (A, len-1, 0);

Len--;

Balance (A, Len, 1);

}

}

void Main ()

{

int a[100];

for (int i = 0; i<100; i++) {

A[i] = rand ()%1000;

}

Heapsort (A, 100);

Kuaisu (A, 100);

int *b = Shellsort (A, 100);

int *b = Maopao (a,100);

int *b = insert (A, 100);

int a[] = {4,7,3,8,2,9};

int *b = Twoinsert (A, 6);

int *b = Shellsort (A, 6);

for (int i = 0; i<100; i++) {

NSLog (@ "%d", a[i]);

}

}

Common sort C implementation

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.