Ten lines of code to get "bubble sort"

xiaoxiao2021-03-06  122

Java Code:

Class class1

{

Static void bubblesort (int [] a)

{

For (int J = a.length - 1; j> 0; j--) // outr loop (INT i = 0; i a [i 1]) // swap {a [i] = a [i] a [i 1]; A [i 1] = a [i] - a [i 1]; a [i] = a [i] - a [i 1];

}

Public static void main (string [] args)

{

Int [] a = new int [] {- ​​1, -2, 9, 4, 5, 3, 7, 1, 2, 3, 1, 2, 3, -3, 2, -19};

// int [] a = new int in 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 11};

Bubblesort (a);

For (int i = 0; i

{

System.out.println (a [i]);

}

}

}

C # cote:

Class class1

{

Static void bubblesort (int [] a)

{

For (int J = a.length - 1; j> 0; j--) // outr loop (INT i = 0; i a [i 1]) {// a [i] = a [i] a [i 1]; // a [i 1] = a [i] - a [i 1] ; // a [i] = a [i] - a [i 1]; swap (Ref a [i], ref A [i 1]);}}

Static Void Swap (Ref Int X, Ref Int Y)

{

X = x y;

Y = x - y;

x = x - y;

}

Static void main (string [] args)

{

Int [] a = new int [] {- ​​1, -2, 9, 4, 5, 3, 7, 1, 2, 3, 1, 2, 3, -3, 2, -19};

// int [] a = new int in 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 11};

Bubblesort (a);

For (int i = 0; i

{

System.console.writeLine (a [i]);

}

System.console.readline ();

}

}

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

New Post(0)