A fast sort algorithm code sharing _c language

Source: Internet
Author: User

Copy Code code as follows:

/*
* QUICKSORT.C
*
* Created on:2012-4-9
* AUTHOR:LW
*/
#include <stdio.h>
#include <string.h>

typedef struct _STUDENT
{
int id;
Char name[30];
}student,*pstudent;

Student Students[20] =
{
{13, "Fox Gold"},{15, "du"},{8, "Gourd Baby"},{4, "pleasant Goat"},
{14, "Jingle Cat"},{18, "Monkey King"},{3, "Gulliver"},{6, "Coffee Cat"},
{19, "pig"},{11, "programmer"},{5, "Robinson"},{9, "Tuna"},
{10, "careless"},{12, "Week Star"},{1, "Gray wolf"},{2, "Donald Duck"},
{20, "false June"},{16, "Utopia June"},{7, "small two Lang"},{17, "Jia Baoyu"},
};

Students[] is an array that needs to be sorted, length is the number of elements of the students array
void QuickSort (student students[],int length)
{
int i,j,flag = students[0].id;
Student Stutemp;
int t;

if (length>1)
{
for (t=0;t<length;t++)
{
printf ("%d", students[t].id);
}
printf ("\ n");

Select the division point for the partition.
for (i=0,j=length-1;i<j;)
{
for (; students[i].id<flag;i++);
for (; students[j].id>flag;j--);

Stutemp.id = students[j].id;
strcpy (Stutemp.name,students[j].name);

Students[j].id = students[i].id;
strcpy (Students[j].name,students[i].name);

Students[i].id = stutemp.id;
strcpy (Students[i].name,stutemp.name);
}

Print sort Process
for (t=0;t<length;t++)
{
printf ("%d", students[t].id);
}
printf ("----sorted \ n-----------------------------------axis:%d subscript:%d\n", flag,j);

To partition and recursively call the quicksort, to operate in the original array space
QuickSort (&students[0],j+1);
QuickSort (&students[j+1],length-j-1);
}
}

int main ()
{
int i;

Print before sorting
printf ("Before: \ n");
for (i=0;i<20;i++)
{
printf ("%d%s\t", students[i].id,students[i].name);
if (!) ( (i+1)%5))
{
printf ("\ n");
}
}

QuickSort (students,20);

Print after sorting
printf ("__________________________________________________________________\n sorted: \ n");
for (i=0;i<20;i++)
{
printf ("%d%s\t", students[i].id,students[i].name);
if (!) ( (i+1)%5))
{
printf ("\ n");
}
}

return 0;
}

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.