Sorting Algorithm C ++ implementation

Source: Internet
Author: User

Bubble Sorting:

Void bubblesort (int * arr, int size)
{
Int temp;
Int last = size-1;
Bool sorted = true;

Do {
Sorted = true;
For (INT I = 0; I <last; I ++ ){


// Swap elements if the higher index element is
// Greater than the smaller index Element

If (ARR [I]> arr [I + 1]) {
Temp = arr [I];
Arr [I] = arr [I + 1];
Arr [I + 1] = temp;
Sorted = false;
}

}

Last --;
} While (! Sorted );
Return;
}

 

========================================================== =

 

 

Insert sorting:

Void insertionsort (int * arr, int size)
{
For (INT I = 1; I <size; I ++)
{
Int v = arr [I];
Int J = I;
While (J! = 0 & arr [J-1]> V)
{
Arr [J] = arr [J-1];
J = J-1;
}
Arr [J] = V;
}
Return;
}

 

 

========================================================== ====

 

Merge sort (merge sort)

 

// Part One combine two sorted Array

Static int templist [array_size];

Void Merge (int A [], int P, int Q, int R)
{
Int n1 = Q-p + 1;
Int n2 = r-Q;
Int I, J, K;
Int * Left = new int [N1 + 1]; // & templist [p]; // (int *) malloc (N1 + 1) * sizeof (INT ));//
Int * Right = new int [n2 + 1]; // & templist [Q]; // (int *) malloc (n2 + 1) * sizeof (INT ));
For (I = 0; I <N1; ++ I)
{
Left [I] = A [p + I];
}
For (j = 0; j <N2; ++ J)
{
Right [J] = A [q + 1 + J];
}
Left [I] = 0x7fffffff;
Right [J] = 0x7fffffff;
I = 0;
J = 0;
For (k = P; k <= r; k ++)
{
If (left [I] <= right [J])
{
A [k] = left [I];
I ++;
}
Else
{
A [k] = right [J];
J ++;
}
}
}

// Part Two make the array to two parts
Void merge_sort (int A [], int P, int R)
{
If (P <R)
{
Int q = (p + r)/2;
Merge_sort (A, p, q );
Merge_sort (A, q + 1, R );
Merge (A, P, Q, R );
}
}

Void mergesort (int A [], int R)
{
Merge_sort (A, 0, R );
}

==================================

 

Quick sorting

 

Void quicksort (int A [], int L, int R)
{

If (L> = r)
Return;
 
Int I = l,
J = R + 1;
Int Limit = A [l];
 
 
 
While (true)
{
Do
{
I = I + 1;
} While (A [I] <strong );


Do
{
J = J-1;
} While (A [J]> values );

If (I> = J)
Break;

Swap (A [I], a [J]);
}
 
A [l] = A [J];
A [J] = bytes;
 
Quicksort (A, L, J-1 );
Quicksort (A, J + 1, R );
}


Void quicksort (int * a, int N)
{
Quicksort (A, 0, n-1 );
}

Void swap (Int & I, Int & J)
{
Int temp = 0;
Temp = I;
I = J;
J = temp;
}
======================================

Related Article

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.