#include "stdio.h"
#include "malloc.h"
#include "Stdlib.h"
typedef int KEYTYPE;
#define MAXSIZE 20
typedef struct
{
KeyType key;
}redtype;
typedef struct
{
Redtype r[maxsize+1];
int length;
}sqlist,* sqlist;
void Play_choose (void);//Display Menu
void Creat_list (SqList L);//Create Order table
void Traverse_list (SqList L);//Traversal Order table
int Search_seq (sqlist l,keytype key);//Order Lookup
void Insertsort (SqList L);//Direct Insertion Sort method
int Search_bin (sqlist l,keytype key);//binary find
void QSort (SqList l,int low,int High);//recursive quick-sort
int Partition (sqlist l,int low,int High);//recursive quick-line execution code
void Selectsort (SqList L);//Simple sorting method
void Effervesce (SqList L);//Bubble sort
void Binsertsort (SqList L);//binary Insert Sort
void Main ()
{
int i;
int key=67,sum,sum1;
SqList L;
L= (sqlist) malloc (sizeof (sqlist));
if (l==null)
{
printf ("Dynamic memory allocation failed!") ");
Exit (-1);
}
Play_choose ();//Display Menu
while (true)
{
printf ("\n\t, select the action you want to perform:");
scanf ("%d", &i);
switch (i)//implement User selection action
{
Case 1:Creat_list (L); break;//Create order table
Case 2:printf ("\n\t, select the value of the keyword you want to find:");//Order Lookup
scanf ("%d", &key);
Sum=search_seq (L,key);
if (sum!=0)
{
printf ("\n\t The value of the keyword you are looking for is equal to%d elements \ n", sum);
}
Else
{
printf ("\ n the keyword you are looking for does not exist \ n");
}
Break
Case 3:insertsort (L); Traverse_list (l); break;//Direct Insertion Sort method
Case 4:qsort (l,1,l->length); traverse_list (L); break;//recursive form of quick-row
Case 5:Selectsort (l); Traverse_list (l); break;//Simple Sorting method
Case 6:EFFERVESCE (L); Traverse_list (l); break;//bubble sort
Case 7:binsertsort (L); Traverse_list (l); break;//binary Insert Sort
Case 8:printf ("\n\t Please select the value of the keyword you are looking for:");//binary find
scanf ("%d", &key);
Search_bin (L,key);
Sum1=search_bin (L,key);
if (sum1!=0)
{
printf ("\n\t The key you are looking for is equal to%d elements \ n", sum1);
}
Else
{
printf ("\n\t The keyword you are looking for does not exist \ n");
}
Break
Case 9:play_choose (); traverse_list (L); break;//Clear Screen Display menu
Case 0:Exit (-1);//exit
Default
printf ("\ t you entered an illegal value \ n");
Break
}
}
}
void Play_choose (void)
{
System ("CLS");
printf ("\n\n\n\t\t.......................................\n\n\n");
printf ("\t\t 1-Set up Order Table 2-order lookup \ n");
printf ("\t\t 3-Direct Insert sort 4-quick sort \ n \ nthe");
printf ("\t\t 5-Simple Select sort 6-bubble sort \ n \ nthe");
printf ("\t\t 7-split semi-insert sort 8-binary find \ n");
printf ("\t\t 9-Clear Screen Display menu 0-exit \ n \ nthe");
printf ("\n\n\t\t ... \n\n\n"), and so on.------------------".
}
void Creat_list (SqList L)//Create order table
{
int i;
int Val;
int Len;
printf ("Please enter the length of the order table you want to establish (the first position does not store the element):");
scanf ("%d", &len);
for (i=1;i<=len;i++)
{
printf ("t \ n Please enter the value of%d elements:", i);
scanf ("%d", &val);
l->r[i].key=val;
}
l->length=len;
printf ("\ n");
printf ("\t\t\t Congratulations on your success in building the table \ n");
}
void Traverse_list (SqList L)//Traversal Order table
{
printf ("\t\t Data after Processing:");
int i;
for (i=1;i<=l->length;i++)
{
printf ("%d", l->r[i].key);
}
printf ("\ n");
}
int Search_seq (sqlist l,keytype key)//order Lookup
{
int i;
l->r[0].key=key;
for (i=l->length; l->r[i].key!=key;i--);
return i;
}
void Insertsort (SqList L)//Direct Insertion Sort method
{
int j,i;
for (i=2;i<=l->length;i++)
{
if (L->r[i].key < L->r[i-1].key)
{
l->r[0]=l->r[i];
l->r[i]=l->r[i-1];
for (j=i-2; l->r[0].key<l->r[j].key;j--)
{
l->r[j+1]=l->r[j];
}
l->r[j+1]=l->r[0];
}
}
}
int Search_bin (sqlist l,keytype key)//binary find
{
int low=1;
int high=l->length;
int mid;
while (Low<=high)
{
Mid= (Low+high)/2;
if (Key==l->r[mid].key)
return mid;
else if (Key<l->r[mid].key) high=mid-1;
else low=mid+1;
}
return 0;
}
void QSort (SqList l,int low,int High)//recursive form of fast row
{
Int Pivotloc;
if (Low{
Pivotloc=partition (L,low,high);
QSort (l,low,pivotloc-1);
QSort (L,pivotloc+1,high);
}
}
int Partition (sqlist l,int low,int High)//recursive form of a quick row
{
int PivotKey;
l->r[0]=l->r[low];
pivotkey=l->r[low].key;
while (Low{
while (Low
l->r[low]=l->r[high];
while (Low
l->r[high]=l->r[low];
}
l->r[low]=l->r[0];
return low;
}
void Selectsort (SqList L)//Simple sorting method
{
int k,i,j;
for (I=1;i<=l->length;++i)
{
J=i;
for (k=i+1;k<=l->length;k++)
{
if (L->r[k].key<l->r[j].key)
{
J=k;
}
}
if (I!=J)
{
Redtype k=l->r[i];
l->r[i]=l->r[j];
l->r[j]=k;
}
}
}
void Effervesce (SqList L)//bubble sort
{
int i,j;
for (i=1;i<l->length;i++)
{
for (j=1;j<=l->length-i;j++)
{
if (L->r[j].key>l->r[j+1].key)
{
Redtype temp=l->r[j];
l->r[j]=l->r[j+1];
l->r[j+1]=temp;
}
}
}
}
void Binsertsort (SqList L)//binary Insert Sort
{
int i,j,mid;
int Low,high;
for (i = 2;i <= l->length;i++)
{
l->r[0]=l->r[i];
low = 1;
High = i-1;
while (Low <= high)
{
Mid = (Low+high)/2;
if (L->r[0].key < L->r[mid].key)
High = mid-1;
Else
low = mid+1;
}
for (j = i-1;j >=high+1;j--)
l->r[j+1]=l->r[j];
L->R[HIGH+1] = l->r[0];
}
}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Implementation codes for various sorting algorithms