Algorithm-sort-quick sort. -- Linux general technology-Linux programming and kernel information. The following is a detailed description. /*************************************** *************************
Title: quicksort. c
Author:
Time:
Purpose: quick sorting
Thread: the idea of the algorithm is ......
Comment:
Usage: 1. gcc-o quicksort. c 2./quicksort
**************************************** ************************/
# Include "stdio. h"
# Include
# Define MAX 20/* maximum String Length */
/*----------------------------------------
Function: q_sort ()
Purpose: recursive processing of quick sorting
Arguments:
Returns: The returned value is
----------------------------------------*/
Void q_sort (char * string, int left, int right)
{
Char partition;/* Split element */
Char temp;
Int I, j, k;
If (left <right)/* whether to continue segmentation */
{
I = left;/* leftmost split */
J = right + 1;/* the rightmost split */
Partition = string [left];/* obtain the first element */
Do {
Do {/* Find from left to right */
I ++;
} While (string <Partition );
Do {/* Find from right to left */
J --;
} While (string [j]> partition );
If (I <j)
{
Temp = string;/* Exchange data */
String= String [j];
String [j] = temp;
}
} While (I <j );
Temp = string [left];/* Exchange data */
String [left] = string [j];
String [j] = temp;
Printf ("output result :");
For (k = left; k <= right; k ++)/* output processing string */
Printf ("% c", string [k]);
Printf ("\ n");/* line feed */
Q_sort (string, left, J-1);/* fast sort recursive call */
Q_sort (string, j + 1, right);/* fast sort recursive call */
}
}
/* ===================================================== ======
Main Program: enter a string and sort the string
========================================================== ===== */
Int main (int argc, char * argv [])
{
Char string [MAX];/* string array */
Int count;/* string length */
Printf ("Enter the string to be sorted => ");
Gets (string);/* read string */
Count = strlen (string);/* calculate the string length */
Quick (string, count);/* quick sorting */
/* Output the sorted string */
Printf ("\ n output sorting result: [% s] \ n", string );
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