C language classic algorithm: bubble sort

xiaoxiao2021-03-14  198

#include

#include

#define Ture 1

#define flase 0

Void Bubblesort (int A [], int N)

{

INT I, J;

Int change;

Int temp;

For (i = n-1, change = Ture; i> = 1 && change; i -)

{

Change = flase;

For (j = 1; j <= i; j )

IF (a [j]> a [j 1])

{

Temp = a [j];

a [j] = a [j 1];

A [J 1] = TEMP;

Change = TURE;

}

}

}

Void Print (Int a [], int N)

{

INT I;

For (i = 1; i <= n; i )

Printf ("A [% D] =% D", I, A [I]);

}

Main ()

{

Int a [100];

INT I, N;

Printf ("/ ninput the length: / n");

Scanf ("% d", & n);

Printf ("/ Ninput the number: / n");

For (i = 1; i <= n; i )

Scanf ("% d", & a [i]);

Printf ("/ nbefore: / n");

Print (a, n);

Bubblesort (A, N);

Printf ("/ NAFTER: / N");

Print (a, n);

Getch ();

Printf ("Press Any Key To Continue ...");

CLRSCR ();

}

转载请注明原文地址:https://www.9cbs.com/read-129301.html

New Post(0)