Several algorithms commonly used in C language

Source: Internet
Author: User


#include <stdio.h>


Bubble sort
void Maopao (int arr[],int len) {
for (int i=0;i<len-1;i++) {
for (int j=0;j<len-i-1;j++) {
if (Arr[j] > arr[j+1]) {
int temp = Arr[j];
ARR[J] = arr[j+1];
ARR[J+1] = temp;
}
}
}
}


Select sort
void Selectsort (int arr[],int len) {
int temp;
for (int i=0;i<len-1;i++) {
for (int j=i+1;j<len;j++) {
if (Arr[i] > Arr[j]) {
temp = Arr[i];
Arr[i] = Arr[j];
ARR[J] = temp;
}
}
}

}


The binary algorithm is also called a binary lookup algorithm
int twosort (int * Listdata,int listlength)
{
int i = 0;
int j = 0;
for (i=1;i<=listlength;i++)
{
Int TMP = Listdata[i];
int low = 0;
int hight = i-1;
int mid;
while (Low <= hight)
{
Mid = (low+hight)/2;
if (tmp > Listdata[mid])
low = mid+1;
Else
Hight = mid-1;
}
for (j=i-1;j>=low;j--)
LISTDATA[J+1]=LISTDATA[J];
listdata[low]=tmp;
}
return 0;
}







int main () {
int a[9] = {1,3,5,7,6,8,9,2,4};
printf ("\ n bubble sort source data \ n");
for (int g=0;g<9;g++) {
printf ("%d\t", A[g]);
}
Maopao (a,9);
printf ("\ n bubble sorted data \ n");
for (int i=0;i<9;i++)
printf ("%d\t", A[i]);


int b[9] = {1,3,5,7,6,8,9,2,4};
printf ("\ n Select sort source data \ n");
for (int g=0;g<9;g++) {
printf ("%d\t", B[g]);
}
Selectsort (b,9);
printf ("\ n select sorted data \ n");
for (int i=0;i<9;i++)
printf ("%d\t", B[i]);


int c[9] = {1,3,5,7,6,8,9,2,4};
printf ("\ n binary algorithm source data \ n");
for (int g=0;g<9;g++) {
printf ("%d\t", C[g]);
}
Twosort (c,9);
printf ("\ n binary algorithm after data \ n");
for (int i=0;i<9;i++)
printf ("%d\t", C[i]);
return 0;
}

Several algorithms commonly used in C language

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.