As the name implies, reversing the order is to reorder the contents of the original array in the reverse sequence. The inverse sorting algorithm is often used in our program development. The basic idea of reverse ordering is simply to swap the last element of the array with the first one, the second and the second to swap, and so on, until all the elements are reversed. Let's look at an example.
1 /**2 * Reverse Sort algorithm instance3 */4 Public classReversesort {5 Public Static voidMain (string[] args) {6 //Create an array7 int[] Array = {Ten, -, -, +, -, -, - };8 //create an object that reverses the sort class9Reversesort Sorter =NewReversesort ();Ten //The method that invokes the sort object reverses the array One Sorter.sort (array); A } - - /** the * Direct selection of the sorting method - * - * The array to sort - */ + Public voidSortint[] Array) { -System. out. println ("The original contents of the array:"); +Showarray (array);//the array value before the output is sorted A inttemp; at intLen =Array.Length; - for(inti =0; I < Len/2; i++) { -temp =Array[i]; -Array[i] = Array[len-1-i]; -Array[len-1-I] =temp; - } inSystem. out. println ("The contents of the array after inversion:"); -Showarray (array);//output sorted array values to } + - /** the * Show all elements of an array * * $ * @param arrayPanax Notoginseng * The array to display - */ the Public voidShowarray (int[] Array) { + for(intI:array) {//The foreach format iterates through the array ASystem. out. Print ("\ t"+ i);//output each array element value the } +System. out. println (); - } $}
Conv New, published in the blog Park
Reprint please indicate the source, welcome mail exchange:[email protected]
Reverse sort of Java