C + + data structures and algorithms-select sort/bubble sort

Source: Internet
Author: User


Bubble sort

#include <stdio.h>
void Bubblesort (int *a,int n);

int main (void)
{
int k;
int a[10]={2,4,6,8,0,1,3,5,7,9};
for (k=0;k<10;k++)
{
if (k==9)
printf ("%d\n", A[k]);
Else
printf ("%d,", a[k]);
}
Bubblesort (a,10);
for (k=0;k<10;k++)
{
if (k==9)
printf ("%d\n", A[k]);
Else
printf ("%d,", a[k]);
}
return 0;
}

void Bubblesort (int *a,int N)
{
int i,j,t;
for (i=0;i<n-1;i++)
for (j=0;j<n-i-1;j++)
{
if (a[j]>a[j+1])
{
T=A[J];
A[J]=A[J+1];
a[j+1]=t;
}
}
}

Select sort

#include <stdio.h>
void Selectsort (int *a,int n);
int main (void)
{
int k;
int x[10]={2,4,6,8,0,1,3,5,7,9};
for (k=0;k<10;k++)
printf ("%d", x[k]);

printf ("\ n");
Selectsort (x,10);
for (k=0;k<10;k++)
printf ("%d", x[k]);
printf ("\ n");
return 0;
}

void Selectsort (int *a,int N)
{
int i,j,t;
int min;
for (i=0;i<n-1;i++)
{
Min=i;
for (j=i+1;j<n;j++)
{
if (A[j]<a[min])
Min=j;
}
T=a[i];
A[i]=a[min];
a[min]=t;
}
}

Okay, the above is the cloud Habitat Community small partners for you to organize an example of the C language sorting method, I hope that these two algorithms can be helpful to you beginners.

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.