After the written examination, summarize several sorting items

Source: Internet
Author: User

I 've gone to several companies for the last few days and found that there are all sorts. So I want to sort them all today, and I have called them in vs6.0.

This evening I summarized two orders.

1) Select sorting: place the maximum or minimum value on the top of each round

2) Bubble Sorting: Find the maximum or minimum sink to the bottom of each round

 

Select sortCode:

Void selectionsort (INT arr [], int N) // select sorting
{
Int smallindex;
Int pass, J;
Int temp;
 
For (pass = 0; pass <n-1; pass ++)
{
Smallindex = pass;
For (j = pass + 1; j <n; j ++)
If (ARR [J] <arr [smallindex])
Smallindex = J;
If (smallindex! = Pass)
{
Temp = arr [pass];
Arr [pass] = arr [smallindex];
Arr [smallindex] = temp;
}
}
}

 

 

Bubble Sorting code:

Void bubblesort (INT arr [], int N) // Bubble Sorting
{
Int bigindex;
Int pass, J;
Int temp;
 
For (pass = n-1; pass> 0; pass --)
{
Bigindex = pass;
For (j = 0; j <= pass; j ++)
{
If (ARR [J]> arr [J + 1])
{
Temp = arr [J];
Arr [J] = arr [J + 1];
Arr [J + 1] = temp;
}
}
}
}

 

 

Test code:

Void main ()
{
Int arr [15];
Int I;
Cout <"original array" <Endl;
 
For (I = 0; I <15; I ++)
{
Arr [I] = rand ();
Cout <arr [I] <"";
}
Cout <Endl;
 
// Selectionsort (ARR, 15 );
Bubblesort (ARR, 15 );
Cout <"sorted array" <Endl;
For (I = 0; I <15; I ++)
Cout <arr [I] <"";
Cout <Endl;
}

The next few orders will be processed tomorrow. Take a rest first, and O (∩) O ~

 

 

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.