New atmosphere, another new learning C came.
Bubble sort, the basis of the foundation, the principle is not verbose.
Display () in the code is an array of display functions, sort_bubble () is a direct implementation of the ordering, details () to drive the picture display.
Pseudo code:
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#define LENGTH 20
Const WORD Fore_blue = foreground_blue| foreground_intensity;
Const WORD Fore_green = foreground_green| foreground_intensity;
Const WORD fore_red = foreground_red| foreground_intensity;
Const WORD Fore_white = foreground_red | foreground_green| Foreground_blue;
void sort_bubble (int *a);
void display (int *a);
void details (int *a);
void display (int *a)
{
int i;
for (i=0;i<length;i++)
{
printf ("%2d", A[i]);
}
printf ("\ n");
}
void sort_bubble (int *a)
{
int i,j,k,temp;
for (i=0;i<length-1;i++)
{
I=r;
for (j=0;j<length-i-1;j++)
{
if (a[j]>a[j+1])
{
TEMP=A[J];
A[J]=A[J+1];
A[j+1]=temp;
k++;
}
}
if (k==0)
{
Break
}
}
}
int main ()
{
int array[length]={20,62,30,50,80,37,40,22,55,44,
77,85,18,44,90,73,26,10,46,64};
int array2[length]={20,62,30,50,80,37,40,22,55,44,
77,85,18,44,90,73,26,10,46,64};
printf ("Before sort:\n");
Display (array);
Sort_bubble (array);
printf ("Success sort:\n");
Display (array);
printf ("Press to display details:\n");
Getch ();
Details (array2);
Getch ();
return 0;
}
void details (int *a)
{
int i,j,k,temp;
HANDLE Outhandle=getstdhandle (Std_output_handle);
COORD xy={0,0};
Xy. y=5;
Display (a);
for (i=0;i<length-1;i++)
{
k=0;
for (j=0;j<length-i-1;j++)
{
Sleep (100);
Xy. x=j*3;
Setconsoletextattribute (Outhandle, fore_red);
SetConsoleCursorPosition (OUTHANDLE,XY);
printf ("%2d%2d", a[j],a[j+1]);
if (a[j]>a[j+1])
{
TEMP=A[J];
A[J]=A[J+1];
A[j+1]=temp;
Sleep (100);
Xy. x=j*3;
SetConsoleCursorPosition (OUTHANDLE,XY);
printf ("");
SetConsoleCursorPosition (OUTHANDLE,XY);
printf ("%2d%2d", a[j],a[j+1]);
k++;
}
Sleep (100);
Xy. x=j*3;
SetConsoleCursorPosition (OUTHANDLE,XY);
Setconsoletextattribute (Outhandle, fore_white);
printf ("%2d%2d", a[j],a[j+1]);
}
Xy. x=3* (length-1-i);
SetConsoleCursorPosition (OUTHANDLE,XY);
Setconsoletextattribute (Outhandle, Fore_green);
printf ("%2d", a[length-i-1]);
if (k==0)
{
Break
}
}
Xy. x=0;
Xy. y=5;
SetConsoleCursorPosition (OUTHANDLE,XY);
Setconsoletextattribute (Outhandle, Fore_green);
Display (a);
CloseHandle (Outhandle);
}
A bubble sorting algorithm for C language implementation