In the interview or in the written test often encountered, the code is not to write also remember how the idea line. Use a pen and paper to draw well, the most classic sort.
8642 Quick Sort
Time limit: 1000MS memory limit: 1000K
question types: programming Language: Unlimited
Describes a quick sort with functions and outputs the result of sorting after each partition
Input first line: keyboard input the number of key to be sorted n the second row: Enter N to sort the keywords, separated by a space data output each row outputs the results of each order, separated by a space between the data
Sample Input 10 5 4 8 0 9 3 2 6 7 1
Sample Output 1 4 2 0 3 5 9 6 7 8
0 1 2 4 3 5 9 6 7 8
0 1 2 4 3 5 9 6 7 8
0 1 2 3 4 5 9 6 7 8
0 1 2 34 5 8 6 7 9
0 1 2 3 4 5 7 6 8 9
0 1 2 3 4 5 6 7 8 9
Hint
Author YQM
Answer:
#include <stdio.h>
#include <malloc.h>
#define OK 1
#define ERROR 0
#define ELEMTYPE INT
typedef struct
{
int *elem;
int length;
int listsize;
}sqlist;
int initlist_sq (sqlist &l,int m)
{
L.elem= (elemtype*) malloc (m*sizeof (elemtype));
if (! L.elem)
return ERROR;
l.length=0;
L.listsize=m;
return OK;
}
int load_sq (sqlist &l)
{
int i;
if (l.length==0)
printf ("The List is empty!");
Else
{
for (i=0;i<l.length;i++)
printf ("%d", l.elem[i]);
}
printf ("\ n");
return OK;
}
int Partition (sqlist &l,int low,int High)
{
int M,pivotkey=l.elem[low];
while (Low{
while (Lowhigh--;
{M=l.elem[low]; L.elem[low]=l.elem[high]; L.elem[high]=m;}
while (Lowlow++;
{M=l.elem[low]; L.elem[low]=l.elem[high]; L.elem[high]=m;}
}
return low;
}
void QSort (SqList &t,int low,int High)
{
int pivotloc;
if (Low{
Pivotloc=partition (T,low,high);
LOAD_SQ (T);
QSort (t,low,pivotloc-1);
QSort (T,pivotloc+1,high);
}
}
int main ()
{
SqList T;
int m,i;
Elemtype e, X;
scanf ("%d", &m);
if (INITLIST_SQ (t,m))//Determine if the order table was created successfully
{
for (i=0;i<m;i++)
{
scanf ("%d", &x);
T.elem[i] = x;
}
T.length = m;
QSort (t,0,m-1);
}
}
8642 Quick Sort