//C language:two sorting algorithms:Bubble Sort and Election Sort
#include <stdio.h>
#include<stdlib.h>
void sort_elect(int* a, int len);
void sort_bubbling(int* a, int len);
int main(void)
{
system("color f0");//Suitable for windos system.
int a[5] = { 3,2,5,1,4 };
sort_bubbling(a, 5);
int i;
for (i = 0; i < 5; i++)
{
printf("%d ", a[i]);
}
return 0;
}
void sort_elect(int *a, int len)
{
int i, j,temp;
for (i = 0; i < len - 1; i++)
{
for (j = i + 1; j < len; j++)
{
if (a[i] > a[j])
{
temp = a[j];
a[j] = a[i];
a[i] = temp;
}
}
}
return;
}
void sort_bubbling(int* a, int len)
{
int i, j, temp;
for (i = 0; i < len - 1; i++)
{
for (j = 0; j < len-1-i; j++)
{
if (a[j + 1] < a[j])
{
temp = a[j + 1];
a[j + 1] = a[j];
a[j] = temp;
}
}
}
return;
}