To sort an integer element in the ArrayList collection in ascending order:
Package org.hyp.test; Import java.util.ArrayList; Import Java.util.Random; public class Mysort {private arraylist<integer> list, public arraylist<integer> getlist () {return list;} PU Blic mysort (int num, int mod) {list = new arraylist<integer> (num); for (int i=0;i<num;i++) {List.add (new Integer (Math.Abs () (New Random (). Nextint ()% mod + 1)); }/* * "bubble sort" starts with the first element, compare adjacent two elements, if the No. 0 element is larger than 1th, then swap both positions, and then move right one position, and then compare adjacent * Two elements, assuming that the total number of elements is n, then after the first round of traversal after N-1, the largest element has been ranked at the last mark bit, The second round compares the remaining elements except it, the secondary large elements in the penultimate mark bit, traversing N-2 times, and so on * * public void Bubblesort () {for (int i=0;i<list.size () -1;i++) {for (int j= I+1;j<list.size (); j + +) {if (((integer) list.get (i)). Intvalue () > ((Integer) List.get (j)). Intvalue ()) {Swap (I, J) ; /* "Select Sort" to set a minimum element flag bit (the initial value is marked with the number No. 0 element), comparing elements No. 0 and 1th, move the marker bit to a smaller element, and then * continue to compare with all the following elements, and after traversing it, the flag bit will point to the smallest element. At this point, the element and the No. 0 element position are exchanged, and the second round of traversal is started, at which point the minimum * flag bit initial value is the leftmost element of the set to be traversed, followed by a loop. * Note that the improvement of the bubble sort is not to swap the position after each comparison, but to set the tag bit, compare a round before exchanging position, determine the minimum element/public void Selectsort () {for (int i=0;i<list.size ()-1; i++) { int minindex = i; for (int j=i+1;j<list.size (), j + +) {if ((integer) List.get (j)). Intvalue () <= ((integer) list.get (Minindex)). Intvalue ()) Minindex = j; Swap (Minindex, i); }/* "Insert Sort" uses a local sort that does not appear in bubbling and selection. From the middle of the sort process, it is understood that all elements on the left side of an intermediate element are already locally ordered, that is, each element is smaller than the element before it, but the final position of these elements is not yet determined because there are several elements outside that are not involved in the row order. You need to start at the back of the rightmost element in a sequence of locally ordered elements, to empty its own position (where the element is stored in temporary space), compare it to each element in the sequence, and if the last one is larger than it, move the elements that are larger than it to the right one position. The element is then inserted into the vacated position. */public void Insertsort () {for (int i=1;i<list.size (); i++) {for (int j=i;j>0 && ((Integer) list.get (j-1) &G t;= (Integer) List.get (j)); j--) {Swap (j-1, j);}} * * "Hill Sort" it divides the sequence into subsequence by setting increments, inserting the sequences into the sequence, decreasing the increment (the interval between elements and elements * in the child sequence) until 1, and the last time inserting the entire sequence once. It is simple to use the value of the sequence length to be halved as increment, also can adopt other increment sequences such as h=3*h+1/public void Shellsort () {for (int i=list.size ()/2;i>0;i/=2) {for int j= 0;j<i;j++) {Sort (list, j, i);}} }//Tool method, insert sort operation on increment sequence private void sort (arraylist<integer> list, int start, int Inc) {for (int i=start+inc;i<lis T.size (); i+=inc) {for (int j=i;J>=inc) && ((((integer) List.get (j)). Intvalue () < ((integer) list.get (J-inc)). Intvalue ()); J-= Inc) {Swap ( J, J-inc); }}/* Quick sort to find an element in the sequence to do pivot value, divide the sequence into left and right two subsequence, after sorting, the left one group is all greater than the hub value, * A group of all is less than the pivot value. Here it simply uses the elements in the middle of the sequence to do the pivot value, it is then swapped with the position of the first element in the left sequence, * and then the lower pointer scans up from the second element, encounters an element that is smaller than the pivot value, moves up one, and the high pointer scans down from the last element, and the element that is larger than the pivot value moves down one. Finally, when the low pointer position is higher than or equal to the high pointer, the sort completes. Because at this point the high pointer points to the element * is still smaller than the hub value, so it is interchangeable with the first element (that is, the pivot value), then the sequence on the left side of the hub value is all smaller than it, the sequence on the right is all greater than it, and then recursively calls the fast sort of two subsequence/public void QuickSort (arraylist<integer> list, int low, int high) {Integer pivot; int Scanup, Scandown, mid; if (High-Low < = 0) return; else if (high-low = 1) {if (List.get (High) < List.get (low)) {Swap (low, high); Pivot = List.get (mid); Swap (low, mid); Scanup = low + 1; Scandown = high; Do {while (Scanup <= scandown && list.get (scanup) <= pivot) Scanup + + while (List.get (scandown) > Pivot) Scandown--; if (Scanup < Scandown) Swap (scanup, scandown); while (Scanup < scandown); list.seT (Low, List.get (Scandown)); List.set (Scandown, pivot); if (Low < scandown-1) QuickSort (list, low, scandown-1); if (High > Scandown + 1) quickSort (list, Scandown + 1, high); }//tool method to implement two elements in the collection interchange position private void swap (int i1, int i2) {Integer temp = list.get (I1); List.set (I1, List.get (I2)); List.s ET (I2, temp); }//tool method to print each element in the collection, in turn, public void display () {for (Integer i:list) {System.out.print (i + "");} System.out.println (); public static void Main (string[] args) {Mysort ms = new Mysort (m); Ms.display (); Ms.bubblesort (); Ms.selectsort () ; Ms.insertsort (); Ms.shellsort (); Ms.quicksort (Ms.getlist (), 0, 9); Ms.display (); } }