2754:c++ Exercises-Quick sort

Source: Internet
Author: User

2754:c++ exercises-Quick sort time limit:1 Sec Memory limit:128 MB
submit:921 solved:406
[Submit] [Status] [Web Board] Description to a certain number as the standard, the larger than the number of the move to its back, than the number of small move to its front, so that any number behind it is larger than any number in front of it, and then the two sets of the number of repetitions of this process, and finally get an orderly sequence , add: If you move from large to small, you can shift the size of the number to the front of it, and move to the back of it. Quick sort from small to large sort algorithm Description: 1) Set two variables first, last, to make a primary equal to a set of numbers of the number of subscript, Last equals the subscript of the final number of a set of numbers, 2) takes an array element as the key data (here the first number of the set of numbers as the key data), assigns a value to the key, namely key=a[first];3) from last to start the search forward, that is, from the beginning to search forward (last--), Find the first value less than key A[last], will a[last] to a[first];4) Start backward search from first, that is, start backward search (first++), find the first A[first "greater than Key", will A[first] to A[last] ; 5) Repeat 3rd, 4, until first=last, the value of key to A[first];6) the two groups on both sides of the first order, repeat 1, 2, 3, 4, 5 steps, until each group has only one number to sort the end. Now, our topic has come ... Enter a set of integers that arranges this set of integers from small to large. Some of the code is given below, just submit the missing code.

#include <iostream>
using namespace Std;
void Quicksort (int a[],int low,int High)
{
if (Low>=high)
{
Return
}
int first=low;
int Last=high;
int Key=a[first];
while (First<last)
{
///////////////////////////////////////////////////////////////////
/*
Please fill in the missing code in this section
*/
////////////////////////////////////////////////////////////////////
}
A[first]=key;
Quicksort (a,low,first-1);
Quicksort (A,last+1,high);
}


int main ()
{
int i,a[100],x,n=0;
cin>>n;
for (i=0; i<n; i++)
cin>>a[i];
Quicksort (a,0,n-1);
for (i=0; i<=n-1; i++)
cout<<a[i]<< "";
cout<<endl;
return 0;
}

Input

Enter n and n integers

Output

From small to large output

Sample Input
102 1 3 5 4 6 8 7 9 10
Sample Output

while (a[last]>=key&&first<last) {    last--;} A[first]=a[last];while (a[first]<=key&&first<last) {    first++;} A[last]=a[first];

  

2754:c++ Exercises-Quick sort

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.