Download code

Jump to: navigation, search

Back to Insertion_sort_(Java)

Download for Windows: zip

Download for UNIX: zip, tar.gz, tar.bz2

InsertionSort14.java

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Insertion_sort_(Java)?oldid=19164
14 */
15 
16 import java.util.Comparator;
17 import java.util.Random;
18 
19 class InsertionSort14 {
20     public static void insertionSort(int[] a) {
21         for (int i=0; i < a.length; i++) {
22             /* Insert a[i] into the sorted sublist */
23 	    int v = a[i];
24 	    int j;
25 	    for (j = i - 1; j >= 0; j--) {
26 	        if (a[j] <= v) break;
27 	        a[j + 1] = a[j];
28 	    }
29 	    a[j + 1] = v;
30         }
31     }
32 
33 
34     public static void insertionSort(Object[] a) {
35         for (int i=0; i < a.length; i++) {
36             /* Insert a[i] into the sorted sublist */
37             Object v = a[i];
38             int j;
39             for (j = i - 1; j >= 0; j--) {
40                 if (((Comparable)a[j]).compareTo(v) <= 0) break;
41                 a[j + 1] = a[j];
42             }
43             a[j + 1] = v;
44         }
45     }
46 
47     public static void insertionSort(Object[] a, Comparator c) {
48         for (int i=0; i < a.length; i++) {
49             /* Insert a[i] into the sorted sublist */
50             Object v = a[i];
51             int j;
52             for (j = i - 1; j >= 0; j--) {
53                 if (c.compare(a[j], v) <= 0) break;
54                 a[j + 1] = a[j];
55             }
56             a[j + 1] = v;
57         }
58     }
59 
60 
61     public static void main(String[] args) {
62         int size = Integer.parseInt(args[0]);
63         Random rand = new Random();
64         int[] a = new int[size];
65         for(int i=0; i<size; i++) {
66             a[i] = rand.nextInt(size);
67         }
68         insertionSort(a);
69         for(int i=1; i<size; i++) {
70             if (a[i] < a[i-1]) {
71                 System.out.println("ERROR");
72             }
73         }
74     }
75 
76 }


hijacker
hijacker
hijacker
hijacker

InsertionSort15.java

  1 /* The authors of this work have released all rights to it and placed it
  2 in the public domain under the Creative Commons CC0 1.0 waiver
  3 (http://creativecommons.org/publicdomain/zero/1.0/).
  4 
  5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 12 
 13 Retrieved from: http://en.literateprograms.org/Insertion_sort_(Java)?oldid=19164
 14 */
 15 
 16 import java.util.Comparator;
 17 import java.util.Random;
 18 
 19 class InsertionSort15 {
 20     public static void insertionSort(int[] a) {
 21         for (int i=0; i < a.length; i++) {
 22             /* Insert a[i] into the sorted sublist */
 23 	    int v = a[i];
 24 	    int j;
 25 	    for (j = i - 1; j >= 0; j--) {
 26 	        if (a[j] <= v) break;
 27 	        a[j + 1] = a[j];
 28 	    }
 29 	    a[j + 1] = v;
 30         }
 31     }
 32 
 33 
 34     public static void insertionSort(Object[] a) {
 35         for (int i=0; i < a.length; i++) {
 36             /* Insert a[i] into the sorted sublist */
 37             Object v = a[i];
 38             int j;
 39             for (j = i - 1; j >= 0; j--) {
 40                 if (((Comparable)a[j]).compareTo(v) <= 0) break;
 41                 a[j + 1] = a[j];
 42             }
 43             a[j + 1] = v;
 44         }
 45     }
 46 
 47     public static void insertionSort(Object[] a, Comparator c) {
 48         for (int i=0; i < a.length; i++) {
 49             /* Insert a[i] into the sorted sublist */
 50             Object v = a[i];
 51             int j;
 52             for (j = i - 1; j >= 0; j--) {
 53                 if (c.compare(a[j], v) <= 0) break;
 54                 a[j + 1] = a[j];
 55             }
 56             a[j + 1] = v;
 57         }
 58     }
 59 
 60 
 61     public static <T extends Comparable<? super T>>
 62     void insertionSortGeneric(T[] a) {
 63         for (int i=0; i < a.length; i++) {
 64             /* Insert a[i] into the sorted sublist */
 65             T v = a[i];
 66             int j;
 67             for (j = i - 1; j >= 0; j--) {
 68                 if (a[j].compareTo(v) <= 0) break;
 69                 a[j + 1] = a[j];
 70             }
 71             a[j + 1] = v;
 72         }
 73     }
 74 
 75     public static <T>
 76     void insertionSortGeneric(T[] a, Comparator<? super T> c) {
 77         for (int i=0; i < a.length; i++) {
 78             /* Insert a[i] into the sorted sublist */
 79             T v = a[i];
 80             int j;
 81             for (j = i - 1; j >= 0; j--) {
 82                 if (c.compare(a[j], v) <= 0) break;
 83                 a[j + 1] = a[j];
 84             }
 85             a[j + 1] = v;
 86         }
 87     }
 88 
 89     public static void main(String[] args) {
 90         int size = Integer.parseInt(args[0]);
 91         Random rand = new Random();
 92         int[] a = new int[size];
 93         for(int i=0; i<size; i++) {
 94             a[i] = rand.nextInt(size);
 95         }
 96         insertionSort(a);
 97         for(int i=1; i<size; i++) {
 98             if (a[i] < a[i-1]) {
 99                 System.out.println("ERROR");
100             }
101         }
102     }
103 
104 }


hijacker
hijacker
hijacker
hijacker