Sort of C language implementation

Source: Internet
Author: User

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <io.h>
#include <math.h>
#include <time.h>

#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0

#define MAX_LENGTH_INSERT_SORT 7/* For quick sorting, determine if insertion sort threshold is selected */

typedef int STATUS;


#define MAXSIZE 10000/* For the maximum number of arrays to sort, you can modify it as needed */
typedef struct
{
The int r[maxsize+1];/* is used to store the array to sort, r[0] as a sentinel or temporary variable */
The int length;/* is used to record the length of the sequential table */
}sqlist;

/* The index of the array r in the interchange L is the value of I and J */
void swap (SqList *l,int i,int J)
{
int temp=l->r[i];
l->r[i]=l->r[j];
l->r[j]=temp;
}

void print (SqList L)
{
int i;
for (i=1;i<l.length;i++)
printf ("%d,", l.r[i]);
printf ("%d", l.r[i]);
printf ("\ n");
}

/* Order table L for Exchange sort (bubble sort beginner) */
void BubbleSort0 (SqList *l)
{
int i,j;
for (i=1;i<l->length;i++)
{
for (j=i+1;j<=l->length;j++)
{
if (L->r[i]>l->r[j])
{
Swap (L,I,J);/* Swap l->r[i] with L->R[J] */
}
}
}
}

/* bubble sort of order table L */
void Bubblesort (SqList *l)
{
int i,j;
for (i=1;i<l->length;i++)
{
for (j=l->length-1;j>=i;j--)/* Note that J is looping from back to forward */
{
if (l->r[j]>l->r[j+1])/* If the former is greater than the latter (note the difference between this and the previous algorithm) */
{
Swap (l,j,j+1);/* Swap l->r[j] with l->r[j+1] */
}
}
}
}

/* Improved bubbling algorithm for sequential table L */
void BubbleSort2 (SqList *l)
{
int i,j;
Status flag=true;/* flag used as marker */
For (i=1;i<l->length && flag;i++)/* If flag is true indicates there was a data exchange, otherwise stop looping */
{
flag=false;/* Initial to FALSE */
for (j=l->length-1;j>=i;j--)
{
if (l->r[j]>l->r[j+1])
{
Swap (l,j,j+1);/* Swap l->r[j] with l->r[j+1] */
flag=true;/* If there is data exchange, flag is true */
}
}
}
}


/* Sort the Order table L as a simple selection */
void Selectsort (SqList *l)
{
int i,j,min;
for (i=1;i<l->length;i++)
{
Min = i;/* defines the current subscript as the lowest value subscript */
for (j = i+1;j<=l->length;j++)/* data after loop */
{
if (L->r[min]>l->r[j])/* If there is a keyword less than the current minimum value */
Min = j;/* assigns the subscript of this keyword to min */
}
if (i!=min)/* If min is not equal to I, the description finds the minimum value, swap */
Swap (l,i,min);/* Swap l->r[i] with l->r[min] */
}
}

/* Direct insert sort for order table L */
void Insertsort (SqList *l)
{
int i,j;
for (i=2;i<=l->length;i++)
{
if (l->r[i]<l->r[i-1])//* need to insert l->r[i] into an ordered sub-table */
{
l->r[0]=l->r[i]; /* Set Sentinel */
for (j=i-1; l->r[j]>l->r[0];j--)
l->r[j+1]=l->r[j]; /* Record and move back */
l->r[j+1]=l->r[0]; /* Insert to the correct location */
}
}
}

/* Sort the Order table L as Hill */
void Shellsort (SqList *l)
{
int i,j,k=0;
int increment=l->length;
Do
{
increment=increment/3+1;/* Increment sequence */
for (i=increment+1;i<=l->length;i++)
{
if (l->r[i]<l->r[i-increment])/* * need to insert l->r[i] into an ordered increment sub-table */
{
l->r[0]=l->r[i]; /* Temporary Presence l->r[0] */
For (j=i-increment;j>0 && l->r[0]<l->r[j];j-=increment)
l->r[j+increment]=l->r[j]; /* record and move back to find the insertion position */
l->r[j+increment]=l->r[0]; /* Insert */
}
}
printf ("Order%d results:", ++k);
Print (*L);
}
while (increment>1);

}


/* Heap Sort ********************************** */

/* Known l->r[s. M], except for L->r[s], the definition of the heap is satisfied, */
/* This function adjusts the l->r[s] keyword to make l->r[s. M] Become a big top pile * *
void Heapadjust (SqList *l,int s,int m)
{
int temp,j;
temp=l->r[s];
for (j=2*s;j<=m;j*=2)/* Filter down on a child with a larger keyword */
{
if (j<m && l->r[j]<l->r[j+1])
++j; /* is the subscript for the larger record in the keyword */
if (Temp>=l->r[j])
Break /* RC should be inserted in position S */
l->r[s]=l->r[j];
S=j;
}
l->r[s]=temp; /* Insert */
}

/* Heap Ordering of order table L */
void Heapsort (SqList *l)
{
int i;
for (i=l->length/2;i>0;i--)/* Build the R in L into a large heap */
Heapadjust (l,i,l->length);

for (i=l->length;i>1;i--)
{
Swap (l,1,i); /* Swap the top record of the heap with the last record of the current unsorted subsequence */
Heapadjust (l,1,i-1); /* Adjust l->r[1..i-1] to Dagen */
}
}

/* **************************************** */


/* Merge Sort ********************************** */

/* will order the sr[i. M] and SR[M+1..N] are merged into ordered Tr[i. N] */
void Merge (int sr[],int tr[],int i,int m,int N)
{
int j,k,l;
For (j=m+1,k=i;i<=m && j<=n;k++)/* Merge the SR record from small to earth into TR */
{
if (Sr[i]<sr[j])
Tr[k]=sr[i++];
Else
Tr[k]=sr[j++];
}
if (i<=m)
{
for (l=0;l<=m-i;l++)
Tr[k+l]=sr[i+l];/* will be the remaining sr[i. M] Copy to TR */
}
if (j<=n)
{
for (l=0;l<=n-j;l++)
Tr[k+l]=sr[j+l];/* will be the remaining sr[j. N] Copy to TR */
}
}


/* Recursive method */
/* Will sr[s. T] Merge sort to tr1[s. T] */
void Msort (int sr[],int tr1[],int s, int t)
{
int m;
int tr2[maxsize+1];
if (s==t)
Tr1[s]=sr[s];
Else
{
M= (s+t)/2;/* will sr[s. T] split into Sr[s. M] and sr[m+1..t] */
Msort (sr,tr2,s,m);/* recursively sr[s. M] Merge into an ordered tr2[s. M] */
Msort (sr,tr2,m+1,t);/* Merge sr[m+1..t] recursively into ordered tr2[m+1..t] */
Merge (tr2,tr1,s,m,t);/* Will tr2[s. M] and tr2[m+1..t] merge to Tr1[s. T] */
}
}

/* Sort order table L as a merge */
void MergeSort (SqList *l)
{
Msort (l->r,l->r,1,l->length);
}

/* Non-recursive method */
/* Merge sub-sequence 22 with adjacent length s in sr[] to tr[] */
void Mergepass (int sr[],int tr[],int s,int N)
{
int i=1;
Int J;
while (I <= n-2*s+1)
{/* 22 merge */
Merge (SR,TR,I,I+S-1,I+2*S-1);
i=i+2*s;
}
if (i<n-s+1)/* Merges the last two sequences */
Merge (Sr,tr,i,i+s-1,n);
else/* If only a single sub-sequence is left at the end */
For (J =i;j <= n;j++)
TR[J] = Sr[j];
}

/* The Order table L is not recursively sorted */
void MergeSort2 (SqList *l)
{
int* tr= (int*) malloc (l->length * sizeof (int)); * * Request extra space */
int k=1;
while (K<l->length)
{
Mergepass (l->r,tr,k,l->length);
k=2*k;/* sub-sequence length doubled */
Mergepass (tr,l->r,k,l->length);
k=2*k;/* sub-sequence length doubled */
}
}

/* **************************************** */

/* Quick Sort ******************************** */

/* Exchange Order table L record of the sub-table, make pivot record in place and return to its location */
/* At this time the record before it is small (smaller) to it. */
int Partition (sqlist *l,int low,int High)
{
int PivotKey;

pivotkey=l->r[low]; /* Pivot record with the first record of the child table */
while (Low{
while (Lowhigh--;
Swap (L,low,high);/* Swap the record smaller than the pivot record to the low end */
while (Lowlow++;
Swap (L,low,high);/* Swap records larger than pivot record to high-end */
}
return low; /* Return to the pivot position */
}

/* L->r[low of subsequence in order table L: High] for quick sorting */
void QSort (SqList *l,int low,int High)
{
int pivot;
if (Low{
Pivot=partition (L,low,high); /* Will l->r[low. High] in Split, calculate pivot value Pivot */
QSort (l,low,pivot-1);/* Recursive ordering of low sub-tables */
QSort (L,pivot+1,high);/* Recursive Ordering of Gaozi */
}
}

/* Quickly sort the Order table L */
void QuickSort (SqList *l)
{
QSort (l,1,l->length);
}

/* **************************************** */

/* Improved quick sort after ******************************** */

/* Fast Sorting optimization algorithm */
int Partition1 (sqlist *l,int low,int High)
{
int PivotKey;

int m = low + (high-low)/2; /* Calculates the subscript for the element in the middle of the array */
if (L->r[low]>l->r[high])
Swap (L,low,high);/* Swap left and right end data to ensure a smaller ieft side */
if (L->r[m]>l->r[high])
Swap (L,HIGH,M);/* swaps intermediate and right data to ensure the middle is small */
if (L->r[m]>l->r[low])
Swap (L,m,low);/* swaps the middle and left data to ensure that the right side is small */

pivotkey=l->r[low]; /* Pivot record with the first record of the child table */
l->r[0]=pivotkey; /* Back up the pivot keyword to l->r[0] */
while (Low{
while (Lowhigh--;
l->r[low]=l->r[high];
while (Lowlow++;
l->r[high]=l->r[low];
}
l->r[low]=l->r[0];
return low; /* Return to the pivot position */
}

void QSort1 (SqList *l,int low,int High)
{
int pivot;
if ((high-low) >max_length_insert_sort)
{
while (Low{
Pivot=partition1 (L,low,high); /* Will l->r[low. High] in Split, calculate pivot value Pivot */
QSort1 (l,low,pivot-1);/* Recursive ordering of low sub-tables */
/* QSort (L,pivot+1,high);/* Recursive Ordering of Gaozi */
low=pivot+1;/* Tail recursion */
}
}
Else
Insertsort (L);
}

/* Quickly sort the Order table L */
void QuickSort1 (SqList *l)
{
QSort1 (l,1,l->length);
}

/* **************************************** */
#define N 9
int main ()
{
int i;

/* int d[n]={9,1,5,8,3,7,4,6,2}; */
int d[n]={50,10,90,30,70,40,80,60,20};
/* int d[n]={9,8,7,6,5,4,3,2,1}; */

SqList L0,l1,l2,l3,l4,l5,l6,l7,l8,l9,l10;

for (i=0;i<n;i++)
L0.r[i+1]=d[i];
L0.length=n;
l1=l2=l3=l4=l5=l6=l7=l8=l9=l10=l0;
printf ("before sorting: \ n");
Print (l0);

printf ("Primary bubble sort: \ n");
BubbleSort0 (&l0);
Print (l0);

printf ("bubble sort: \ n");
Bubblesort (&L1);
Print (L1);

printf ("Improved bubble sort: \ n");
BubbleSort2 (&L2);
Print (L2);

printf ("Select sort: \ n");
Selectsort (&L3);
Print (L3);

printf ("Direct insert sort: \ n");
Insertsort (&L4);
Print (L4);

printf ("Hill sort: \ n");
Shellsort (&L5);
Print (L5);

printf ("heap sort: \ n");
Heapsort (&L6);
Print (L6);

printf ("merge sort (recursive): \ n");
MergeSort (&L7);
Print (L7);

printf ("merge sort (non-recursive): \ n");
MergeSort2 (&L8);
Print (L8);

printf ("Quick sort: \ n");
QuickSort (&L9);
Print (L9);

printf ("Improved quick sort: \ n");
QuickSort1 (&L10);
Print (L10);


/* Big Data sort */
/*
Srand (Time (0));
int max=10000;
int d[10000];
int i;
SqList L0;
for (i=0;i<max;i++)
D[i]=rand ()%max+1;
for (i=0;i<max;i++)
L0.r[i+1]=d[i];
L0.length=max;
MergeSort (L0);
Print (l0);
*/
return 0;
}

Sort of C language implementation

Related Article

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.