C language Bubble, insert, select, fast sort

Source: Internet
Author: User
//============================================================================
Name:sorts.cpp
Author:zhoudan
Version:
Copyright:your Copyright Notice
Description:hello World in C + +, Ansi-style
//============================================================================


#include <iostream>
using namespace Std;


/* Generally, the insertion sort is implemented on an array using In-place. The specific algorithm is described as follows:
Starting with the first element, the element can be thought to have been sorted
Takes out the next element and scans backwards in the sorted sequence of elements
If the element (sorted) is greater than the new element, move the element to the next position
Repeat step 3 until you find where the sorted element is less than or equal to the new element
Inserts a new element into the location
Repeat steps 2~5*/
void Insert_sort (int *a,int N)
{
int i,j,temp;
for (i = 1;i<n;i++)
{
temp = A[i];
j = i-1;
Notice here, if j=i, then j>0; if j=i-1, then j>=0,
A[i] Iterate from the back, find the larger one after the move, and eventually put A[i] into the right place
while (Temp<a[j] && j>=0)
{
if (Temp<a[j])
{
A[J+1] = A[j];
}
j--;
}
A[J+1] = temp;
}
}


/*bubble_sort: Bubble Sort, the idea is to move the largest array element to the far right at every traversal, I just to make J and J-1 compare n-i times.
*
* */
void Bubble_sort (int a[],int N)
{
int i,j,temp;
for (i = 0;i<n;i++)
{
for (j = 1;j<n-i;j++)//note is compared n-i times
{
if (A[j-1]>a[j])
{
temp = a[j-1];
A[J-1] = A[j];
A[J] = temp;
}
}
}
}


Used to select a sort of array operation
Parameter A is an array that needs to be sorted
Parameter n is the number of elements that need to sort the array
void Selectsort (int *a,int N)
{
int i;
Int J;
int temp = 0;
int flag = 0;
for (i = 0; i < n-1 i++)////////////////////////////////
{
temp = A[i];
flag = i;
for (j = i+1 J < n; j)//small loop, used to scan the array backwards and select the decimal
{
if (A[j] < temp)
{
temp = A[j];
flag = j; Subscript of the currently smallest element
}
}
if (flag!= i)///If the smallest element is not the first in the data being filtered, the minimum data is exchanged with the first filtered data
{
A[flag] = A[i];
A[i] = temp;
}
}
}


/* Select sort, through I control the number of comparisons, each time the smallest number of rows to the forefront of the array, and then compare the number of not ordered. That's a good order.
* */
void Choice_sort (int a[],int N)
{
int i,j,min,temp;
for (i = 0;i<n-1;i++)//To control program does not operate on the number of rows already ordered
{
min = i; Subscript of the minimum number
temp = A[i];
for (j = i+1;j<n;j++)//traversal to find the smallest number,
{
if (A[min]>a[j])
{
min = j;
temp = A[j];
}
}
if (min!=i)//When I. When =min, the smallest number is exchanged with the a[i], and the smallest number is at the front.
After n-1, we can arrange the order.
{
A[min] = A[i];
A[i] = temp;
}
}
}


/* Quick sort, using the idea of the binary, to find an intermediate value, and all the other values smaller than it is arranged on its left, greater than its value is arranged on the right side, and then recursively calls
* */
void Quick_sort (int a[],int left,int right)
{
int i,j,temp;
I=left;
J=right;
Temp=a[left];
if (left>right)
Return
while (I!=J)/* Find the final position * *
{
Notice here, because the initial setting is Temp=a[left], so it's time to start with the right first comparison. Otherwise there will be an error.
while (A[j]>=temp && j>i)
j--;
if (j>i)
A[I++]=A[J];
while (A[i]<=temp && j>i)
i++;
if (j>i)
A[j--]=a[i];


}
A[i]=temp;
Quick_sort (a,left,i-1);
Quick_sort (a,i+1,right)/* Recursive right/*
}




void Print_sort (int a[],int N)
{


for (int i = 0;i<n;i++)
printf ("%d", a[i]);
}


int main () {
int a[8] = {2,4,1,6,5,9,3,7};
cout<< "After Choice_sort" <<endl;
Choice_sort (a,8);
Print_sort (a,8);


int b[8] = {2,4,1,6,5,9,3,7};
cout<<endl<< "After Bubble_sort" <<endl;
Bubble_sort (b,8);
Print_sort (b,8);


int c[8] = {2,4,1,6,5,9,3,7};
cout<<endl<< "After Insert_sort" <<endl;
Insert_sort (c,8);
Print_sort (c,8);


int d[8] = {2,4,1,6,5,9,3,7};
cout<<endl<< "After Quick_sort" <<endl;
Quick_sort (d,0,7);
Print_sort (d,8);
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.