Array common sort, lookup algorithm _ algorithm

Source: Internet
Author: User

One: array of commonly used sorting algorithm:

1): Bubble sort (Bubble sorting)

Algorithm idea: The adjacent 2 number comparison size, the small person moves forward, after a trip compares, the smallest number goes to the front, compared

The large number (heavy) slowly sinks down and flows to the back of the array. If there are n numbers, pass the n-1

The number of numbers in the array that have been sorted (ascending).

Program code:

void Bubble_sort (int arr[],int size)

{
int Temp,flag;
int i,j;
for (int i=0;i<size;i++)
{
flag=0;
for (int j=size-1;j>i;j--)
{
if (Arr[j]<arr[j-1])
{
TEMP=ARR[J];
ARR[J]=ARR[J-1];
Arr[j-1]=temp;
flag=1;
}
}
if (flag==0) break;
}
}


2): Exchange sort (swap sorting)

Algorithm idea:

The value of the 1th element is compared to the value of all subsequent elements, if one of the values is greater than the 1th value

Small, to be exchanged. By analogy, the 2nd element is exchanged with all subsequent elements until

N-1 an element.

Program code:

void Swap_sort (int arr[],int size)
{
int temp;
int i,j;
for (i=0;i<size;i++)
for (j=i+1;j<size;j++)
if (Arr[j]<arr[i])
{
Temp=arr[i];
ARR[I]=ARR[J];
Arr[j]=temp;
}
}

3): Select sort (sorting)

Algorithm idea:

Swapping sorts has a disadvantage: if the data behind the series is smaller than the previous data, each trip will

Produces many unnecessary exchanges, wasting system time. The basic idea of choosing a sort is: if there are n numbers, fake

Set the 1th digit to hold the smallest number and compare this number with all subsequent numbers. Use subscript variable K to record

The subscript of the element that contains the minimum number. After a comparison is completed, the arr[0] is exchanged with arr[k]. and then Set

The 2nd place holds the second small number, and so on.

Program code:

void Select_sort (int arr[],int size)
{
int i,j,k;
int temp;
for (i=0;i<size;i++)
{
K=i;
for (j=i+1;j<size;j++)
if (Arr[j]<arr[k])
K=j;
if (k!=i)
Temp=arr[i],arr[i]=arr[k],arr[k]=temp;
}
}

4): Direct insertion sort (insert sorting)

Algorithm idea:

Treat the 1th number as an ordered sequence, and then insert each successive number into the ordered sequence, in turn.

Program code:

void Insert_sort (int arr[],int size)
{
int i,j;
int temp;
for (i=1;i<size;i++)
{
Temp=arr[i];
for (j=i-1;j>=0;j--)
if (Temp<arr[j])
ARR[J+1]=ARR[J];
Else
Break
Arr[j+1]=temp;
}
}

Two: The search algorithm of the array:

1): Sequential search

Algorithm idea:

According to the order of the elements in the array, compare them sequentially to find out where the elements are.

Program code:

int sequencesearch (int arr[],int key,int size)
{
int i;
for (i=0;i<size;i++)
if (Arr[i]==key)
return i;
return-1;
}

2): Binary search

Algorithm idea: This kind of lookup method is for already sorted array of a search way, binary find pole

Greatly improves the performance of the lookup, but it is exchanged at the cost of a sorted array.

Program code:

int binarysearch (int arr[],int key,int size)
{
int low=0,high-size-1,middle;
int count=0;
while (Low<=high)
{
Middle= (Low+high)/2;
count++;
if (Key==arr[middle]
{
printf ("Compare count=%d/n", count);
return middle;
}
else if (Key>arr[middle])
low=middle+1;
Else
high=middle-1;
}
printf ("Compare count=%d/n", count);
return-1;
}

Note: An algorithm for exchanging positions for two elements without the help of intermediate variables:

void Chang (int x,int y)
{
X=x+y;
Y=x-y;
X=x-y;
}

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.