Various sorting algorithms commonly used in IOS development learning

Source: Internet
Author: User

Common sorting algorithms

#include <iostream>

using namespace Std;

typedef int ELEMTYPE;

/*

1. Insert Sort

(1) Direct insertion sorting algorithm

The algorithm thought: divides the equal row sequence into the order and the disorderly two parts, then sequentially inserts the unordered part in the already ordered part, finally

You can form an ordered sequence.

The procedure is as follows:

1) Find out the insertion position of the element L (i) in the table K;

2) The elements before the K element in the table are sequentially shifted back one position;

3) Copy L (i) to L (K).

Time complexity: O (n^2)

*/

void Insertsort (Elemtype arr[], int length)

{

int I, J;

Elemtype Guard; Sentinel

for (i = 1; i < length; ++i)

{

if (Arr[i] < arr[i-1])//Find an element in the unordered section and keep it in order after inserting it into the ordered part

{

Guard = arr[i];//Copy to "Sentinel"

Moves the elements before the first element to one position in turn

for (j = i-1; Arr[j] > guard; j--)

{

Arr[j + 1] = Arr[j];

}

Arr[j + 1] = guard; Copy to insertion location

}

}

}

/*

2. Binary Insert Sort

Linear table for sequential storage using the Sort table

When finding the insertion position, use binary to find

The algorithm idea is:

1) Set binary search scope;

2) Binary Search

3) Moving elements

4) Insert Element

5) Continue operation 1), 2), 3), 4) step until the table is in order.

*/

void Binaryinsertsort (Elemtype arr[], int length)

{

int I, J, low, High, mid;

Elemtype tmp;

for (i = 1; i < length; ++i)

{

TMP = Arr[i]; Copy to Sentinel

Set Binary Lookup range

Low = 0;

High = i;

while (low <= high)//binary find

{

Mid = (low + high)/2;

if (Arr[mid] > TMP)//Find in the left half

{

High = mid-1;

}

Else

{

Low = mid + 1; Find in the right half

}

}

Moving elements

for (j = i-1; J >= High + 1;--j)

{

Arr[j + 1] = Arr[j];

}

Arr[j + 1] = tmp;

}

}

/*

3. Hill (Shell) sort

Basic idea:

First, the table to be sorted into a number of shapes if l[i, i+d, i+2d, ..., I+KD] of the "special" sub-table, respectively, the direct insertion of the sort,

Once the entire table has been "basically ordered," then a direct insert sort is made to the whole record.

Algorithm process:

1) First take a step D1 less than N, all the records in the table are divided into D1 groups, all records with a multiple of D1 are placed in the same group, in each

The direct insertion sort in the group;

2) Then take the second step D2 < D1, repeat step 1

3) until DK = 1, then the last direct insert sort

*/

void Shellsort (Elemtype arr[], int length)

{

int I, j, dk = LENGTH/2;

Elemtype tmp;

while (DK >= 1)//Control step

{

for (i = DK; i < length; ++i)

{

if (Arr[i] < ARR[I-DK])

{

TMP = Arr[i]; Temporary storage

Move back

for (j = i-dk; J >= 0 && tmp < ARR[J]; J-= DK)

{

Arr[j + DK] = Arr[j];

}

Arr[j + DK] = tmp;

}

}

DK/= 2;

}

}

/*

4. Bubble Sorting algorithm

Basic idea:

Suppose that the table to be sorted is N, the value of the adjacent element is compared from backward forward or back 22, and if it is reversed, it is exchanged until the sequence is compared.

Such a return is called a trip bubbling. The element with the larger value "sinks" and the smaller element is "floating".

Time Complexity of O (n^2)

*/

void Bubblesort (Elemtype arr[], int length)

{

int I, J;

Elemtype tmp;

for (i = 0; i < length-1; ++i)//Times

{

for (j = i + 1; j < length; ++j)

{

if (Arr[i] > Arr[j])

{

TMP = Arr[i];

Arr[i] = Arr[j];

ARR[J] = tmp;

}

}

}

}

/*

5. Fast sorting algorithm

Basic idea: Based on the divide-and-conquer method, any element pivot is taken as a datum in the N elements to be sorted, and the sorted table is divided into separate

Two parts l[1..k-1] and l[k+1. N], so that all element values in the first part are less than pivot, and all element values in the second part are greater than pivot,

The datum element is placed on its final position, L (K), which is a quick sequence. And then recursively repeat the process for two sub-tables until each

There is only one element in the section or is empty so that all elements are placed in their final position.

*/

int Partition (elemtype arr[], int left, int. right)

{

Elemtype pivot = Arr[left]; The first element in the current table is a pivot value

while (left < right)

{

Find the position of an element that is smaller than the pivot value from right to left

while (left < right && Arr[right] >= pivot)

{

--right;

}

Arr[left] = Arr[right]; Move elements that are smaller than the pivot value to the left side

Find the position of an element that is larger than the pivot value from left to right

while (left < right && Arr[left] <= pivot)

{

++left;

}

Arr[right] = arr[left];//Move the element larger than the pivot value to the right end

}

Arr[left] = pivot; Place the pivot element in the final position

return left;

}

void QuickSort (Elemtype arr[], int left, int. right)

{

if (left < right)

{

int pivotpos = Partition (arr, left, right); Divided

QuickSort (arr, left, pivotPos-1); Quick sort left Half

QuickSort (arr, Pivotpos + 1, right); Quick sort Right Half

}

}

/*

6. Simple Selection Sorting algorithm

Basic idea:

Assuming that the sort table is L[1...N], the first order of ordering from the table selects the smallest element of the key with the Li Interchange, the initial sort can determine an element's

The final position, so that by n-1 sequencing can make the entire sort table orderly.

*/

void Selectsort (Elemtype arr[], int length)

{

int I, j, Min;

Elemtype tmp;

for (i = 0; i < length-1; ++i)//Need n-1

{

min = i;

for (j = i + 1; j < length; ++j)

{

if (Arr[j] < arr[min])//Each trip Select the lowest index of the element value

{

min = j;

}

}

if (min! = i)//If the Li element value of the trip to the trip finds the smallest element value, then the interchange is exchanged so that the Li value is minimized

{

TMP = Arr[i];

Arr[i] = Arr[min];

Arr[min] = tmp;

}

}

}

/*

7. Heap Sorting algorithm

The heap is defined as follows: N keyword serial number L[1..N] is called a heap, only if the sequence satisfies:

1) L (i) <= L (2i) and L (i) <= L (2i+1) or 2 L (i) >= L (2i) and L (i) >= L (2i+1)

The heap that satisfies the first case, called the small Gan (small top heap);

The heap that satisfies the second case, called the Dagen (large top heap).

*/

void Heapadjust (elemtype *a,int i,int size)//Adjustment heap

{

int lchild = 2 * i; I's left child node number

int rchild = 2 * i + 1; I's right child node number

int max = i; Temp variable

if (i <= SIZE/2)//If I is a leaf node there is no need to adjust

{

if (lchild <= size && a[lchild] > A[max])

{

max = Lchild; The left child is larger than his parents and needs to be adjusted

}

if (rchild <= size && a[rchild] > A[max])

{

max = rchild;//The right child is larger than the parent value and needs to be adjusted

}

if (max! = i)//need to adjust

{

Elemtype tmp = A[max];

A[max] = A[i];

A[i] = tmp;

Heapadjust (A, max, size); Avoid resizing a subtree with Max as the parent node is not a heap

}

}

}

void Buildheap (elemtype *a,int size)//Build Heap

{

for (int i = SIZE/2; I >= 0; i--)//non-leaf node maximum ordinal value is SIZE/2

{

Heapadjust (A, I, size);

}

}

void Heapsort (elemtype *a, int size)//heap Sort

{

Buildheap (a,size);

for (int i = size-1; I >= 0; i--)

{

Swap (a[0], a[i]); Swaps the top of the heap and the last element, that is, each time the largest of the remaining elements is put to the last face

Buildheap (A, i-1); Re-establish the remaining elements as a large top heap

Heapadjust (a,1,i-1); Resize the top node of the heap to become a big top heap

}

}

void Display (Elemtype arr[], int length)

{

for (int i = 0; i < length; ++i)

{

cout << Arr[i] << "";

}

cout << Endl;

}

int main ()

{

Elemtype arr[] = {2, 1, 5, 3, 4, 0, 6, 9,-1, 4, 12};

Insertsort (arr, sizeof (arr)/sizeof (elemtype));

Binaryinsertsort (arr, sizeof (arr)/sizeof (elemtype));

Shellsort (arr, sizeof (arr)/sizeof (elemtype));

Bubblesort (arr, sizeof (arr)/sizeof (elemtype));

QuickSort (arr, 0, sizeof (arr)/sizeof (Elemtype)-1);

Heapsort (arr, sizeof (arr)/sizeof (elemtype));

Display (arr, sizeof (arr)/sizeof (elemtype));

return 0;

}

Various sorting algorithms commonly used in IOS development learning

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.