Several common small algorithms

Source: Internet
Author: User

# Include <iostream>
Using namespace STD;
Void choice_sort (int * arr, int Len );

Void quicksort (int * arr, int start, int begin );
Int main ()
{
Int arr [10] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
Quicksort (ARR, 0, 9 );
For (INT I = 0; I <10; I ++) // asymmetric Boundary
{
Cout <arr [I] <"";
}
Cout <Endl;
Return 0;
}

Void bubble_sort (int * arr, int Len)
{
Int temp;
For (INT I = 0; I <len-1; I ++)
{
For (Int J = 0; j <len-1-i; j ++)
{
If (ARR [J]> arr [J + 1])
{
Temp = arr [J];
Arr [J] = arr [J + 1];
Arr [J + 1] = temp;
}
}
}
}

// The insertion sorting idea is: An if statement contains two cases.
// Compare the minimum value with all other values in the array.

void insert_sort (int * arr, int Len)
{< br> int temp;
int Max;
// number of times that the outer large loop is controlled, isn't it?
for (INT I = 0; I {< br> max = I;
for (Int J = I + 1; j {< br> If (ARR [Max]> arr [J])
{< br> max = J;
}< BR >}< br> If (max! = I)
{< br> temp = arr [I];
arr [I] = arr [Max];
arr [Max] = temp;
}< BR >}

void choice_sort (int * arr, int Len)
{< br> // determine the number of large loops, loop n-1
Int J = 0;
int temp;
for (INT I = 0; I {
J = I;
temp = arr [J + 1];
while (j> = 0 & temp {< br> arr [J + 1] = arr [J];
j --;
}< br> arr [J + 1] = temp;
}< BR >}

Int partition (int * arr, int start, int partition)
{
Int I = start;
Int J = start;
Int temp;
For (; j <strong; j ++)
{
If (ARR [J] <arr [region])
{

Temp = arr [I];
Arr [I] = arr [J];
Arr [J] = temp;
I ++;
}
}
Temp = arr [I];
Arr [I] = arr [region];
Arr [temp] = temp;
Return I; // .. return the Axis Position
}

Void quicksort (int * arr, int start, int begin)
{
Int Q;
If (start <begin)
{
Q = partition (ARR, start, partition );
Quicksort (ARR, start, q-1 );
Quicksort (ARR, q + 1, Baidu );
}
}

// Binary Search
// Reduce the problem by half each time.

int binary_search (INT arr, int Len, int target)
{< br> int Top = 0, Bottom = len-1, mid =-1;
while (top <= bottom)
{< br> mid = (top + bottom) >>1;
If (mid> target)
{< br> bottom = mid-1;
}else if (mid {< br> Top = Mid + 1;
}else
{< br> return mid;
}< BR >}< br> return-1;
}

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.