Home > Back-end >  Want to output directly sort, selection sort, heap sort, quick sort, the former five lines of sortin
Want to output directly sort, selection sort, heap sort, quick sort, the former five lines of sortin

Time:01-07

#include

Void print (int arr [], int n)
{
for(int i=0; iPrintf (" % d ", arr [I]);
printf("\n");
}

//direct insertion sort
Void InsertSort (int arr [], int n)
{
int i,j;
for(i=1; i{
If (arr [I] {
Int t=arr [I];
For (j=I - 1; J>=0 & amp; & Arr [j] & gt; t; J -)
{
Arr=arr [m + 1] [j];
}
Arr [m + 1]=t;
}
If (i<6)
Print (arr, n);
}
}

//quick sort
Void QuickSort (int arr [], int l, int r, int k, int n)
{
If (l{
Int I=l=r j, x=arr [l];
While (i{
While (ij--;
If (iArr [i++]=arr [j];
While (ii++;
If (iArr=arr [j -] [I];
}
Arr=x [I];
If (k<6)
{
Print (arr, n);
K=k + 1;
}
QuickSort (arr, l, I - 1, k, n);
QuickSort (arr, I + 1, r, k, n);

}
}
//selection,
Void SelectSort (int arr [], int n)
{
int i,j,k;
for(i=0; i{
K=I;
For (j=I + 1; j{
If (arr [k] & gt; Arr [j])
K=j;
}
Int t=arr [I];
Arr=arr [I] [k].
Arr [k]=t;
If (i<5)
Print (arr, n);
}
}

//heap
Void swap (int * a, int * b)
{
Int t=* b;
* b=* a;
* a=t;
}
Void MaxHeapify (int arr [], int start, int the end)
{
Int dad=start;
Int son=dad * 2 + 1;
While (son<=end)
{
If (son + 1 & lt;=end& & Arr [son] Son++;
If (arr [dad] & gt; Arr [son])
return;
The else
{
Swap (& amp; Arr [dad], & amp; Arr [son]);
Dad=son;
Son=dad * 2 + 1;
}
}
}
Void HeapSort (int arr [], int n)
{
int i;
For (I=n/2-1; I>=0; I -)
MaxHeapify (arr, I, n - 1);
For (I=n - 1; I> 0; I -)
{
Swap (& amp; Arr [0], & amp; Arr [I]);
MaxHeapify (arr. Zero, I - 1);
If (i<6)
Print (arr, n);
}
}
Int main ()
{
int n;
Printf (" (case arr [8]=,3,4,6,2,8,5,9 {0}) \ n please enter a number less than 5: ");
The scanf (" % d ", & amp; N);
Int arr [100].
Printf (" please input data: ");
for(int i=0; iThe scanf (" % d ", & amp; Arr [I]);
Printf (" before ordering: \ n ");
Print (arr, n);
Printf (" top five times: \ n ");
//InsertSort (arr, n);
Int k=1;
QuickSort (arr. Zero, n - 1, k, n);
//SelectSort (arr, n);
//HeapSort (arr, n);
return 0;
}
  • Related