Download code

From LiteratePrograms

Jump to: navigation, search

Back to Insertion_sort_(Java)

Download for Windows: zip

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

InsertionSort14.java

 1 /* Copyright (c) 2010 the authors listed at the following URL, and/or
 2 the authors of referenced articles or incorporated external code:
 3 http://en.literateprograms.org/Insertion_sort_(Java)?action=history&offset=20081217213257
 4 
 5 Permission is hereby granted, free of charge, to any person obtaining
 6 a copy of this software and associated documentation files (the
 7 "Software"), to deal in the Software without restriction, including
 8 without limitation the rights to use, copy, modify, merge, publish,
 9 distribute, sublicense, and/or sell copies of the Software, and to
10 permit persons to whom the Software is furnished to do so, subject to
11 the following conditions:
12 
13 The above copyright notice and this permission notice shall be
14 included in all copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 
24 Retrieved from: http://en.literateprograms.org/Insertion_sort_(Java)?oldid=15633
25 */
26 
27 import java.util.Comparator;

28 
29 import java.util.Random;

30 
31 
32 class InsertionSort14 {
33     public static void insertionSort(int[] a) {
34         for (int i=1; i < a.length; i++) {
35             /* Insert a[i] into the sorted sublist */
36 	    int v = a[i];
37 	    int j;
38 	    for (j = i - 1; j >= 0; j--) {
39 	        if (a[j] <= v) break;
40 	        a[j + 1] = a[j];
41 	    }
42 	    a[j + 1] = v;
43 
44         }
45     }
46 
47 
48     public static void insertionSort(Object[] a) {
49         for (int i=0; i < a.length; i++) {
50             /* Insert a[i] into the sorted sublist */
51             Object v = a[i];
52             int j;
53             for (j = i - 1; j >= 0; j--) {
54                 if (((Comparable)a[j]).compareTo(v) <= 0) break;
55                 a[j + 1] = a[j];
56             }
57             a[j + 1] = v;
58         }
59     }
60 
61 
62     public static void insertionSort(Object[] a, Comparator c) {
63         for (int i=0; i < a.length; i++) {
64             /* Insert a[i] into the sorted sublist */
65             Object v = a[i];
66             int j;
67             for (j = i - 1; j >= 0; j--) {
68                 if (c.compare(a[j], v) <= 0) break;
69                 a[j + 1] = a[j];
70             }
71             a[j + 1] = v;
72         }
73     }
74 
75 
76     public static void main(String[] args) {
77         int size = Integer.parseInt(args[0]);
78         Random rand = new Random();
79         int[] a = new int[size];
80         for(int i=1; i<size; i++) {
81             a[i] = rand.nextInt(size);
82         }
83         insertionSort(a);
84         for(int i=1; i<size; i++) {
85             if (a[i] < a[i-1]) {
86                 System.out.println("ERROR");
87             }
88         }
89     }
90 
91 }
92 


InsertionSort15.java

  1 /* Copyright (c) 2010 the authors listed at the following URL, and/or
  2 the authors of referenced articles or incorporated external code:
  3 http://en.literateprograms.org/Insertion_sort_(Java)?action=history&offset=20081217213257
  4 
  5 Permission is hereby granted, free of charge, to any person obtaining
  6 a copy of this software and associated documentation files (the
  7 "Software"), to deal in the Software without restriction, including
  8 without limitation the rights to use, copy, modify, merge, publish,
  9 distribute, sublicense, and/or sell copies of the Software, and to
 10 permit persons to whom the Software is furnished to do so, subject to
 11 the following conditions:
 12 
 13 The above copyright notice and this permission notice shall be
 14 included in all copies or substantial portions of the Software.
 15 
 16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 19 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 20 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 21 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 22 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 23 
 24 Retrieved from: http://en.literateprograms.org/Insertion_sort_(Java)?oldid=15633
 25 */
 26 
 27 import java.util.Comparator;

 28 
 29 import java.util.Random;

 30 
 31 
 32 class InsertionSort15 {
 33     public static void insertionSort(int[] a) {
 34         for (int i=1; i < a.length; i++) {
 35             /* Insert a[i] into the sorted sublist */
 36 	    int v = a[i];
 37 	    int j;
 38 	    for (j = i - 1; j >= 0; j--) {
 39 	        if (a[j] <= v) break;
 40 	        a[j + 1] = a[j];
 41 	    }
 42 	    a[j + 1] = v;
 43 
 44         }
 45     }
 46 
 47 
 48     public static void insertionSort(Object[] a) {
 49         for (int i=0; i < a.length; i++) {
 50             /* Insert a[i] into the sorted sublist */
 51             Object v = a[i];
 52             int j;
 53             for (j = i - 1; j >= 0; j--) {
 54                 if (((Comparable)a[j]).compareTo(v) <= 0) break;
 55                 a[j + 1] = a[j];
 56             }
 57             a[j + 1] = v;
 58         }
 59     }
 60 
 61 
 62     public static void insertionSort(Object[] a, Comparator c) {
 63         for (int i=0; i < a.length; i++) {
 64             /* Insert a[i] into the sorted sublist */
 65             Object v = a[i];
 66             int j;
 67             for (j = i - 1; j >= 0; j--) {
 68                 if (c.compare(a[j], v) <= 0) break;
 69                 a[j + 1] = a[j];
 70             }
 71             a[j + 1] = v;
 72         }
 73     }
 74 
 75 
 76     public static <T extends Comparable<? super T>>
 77     void insertionSortGeneric(T[] a) {
 78         for (int i=1; i < a.length; i++) {
 79             /* Insert a[i] into the sorted sublist */
 80             T v = a[i];
 81             int j;
 82             for (j = i - 1; j >= 0; j--) {
 83                 if (a[j].compareTo(v) <= 0) break;
 84                 a[j + 1] = a[j];
 85             }
 86             a[j + 1] = v;
 87         }
 88     }
 89 
 90 
 91     public static <T>
 92     void insertionSortGeneric(T[] a, Comparator<? super T> c) {
 93         for (int i=1; i < a.length; i++) {
 94             /* Insert a[i] into the sorted sublist */
 95             T v = a[i];
 96             int j;
 97             for (j = i - 1; j >= 0; j--) {
 98                 if (c.compare(a[j], v) <= 0) break;
 99                 a[j + 1] = a[j];
100             }
101             a[j + 1] = v;
102         }
103     }
104 
105 
106     public static void main(String[] args) {
107         int size = Integer.parseInt(args[0]);
108         Random rand = new Random();
109         int[] a = new int[size];
110         for(int i=1; i<size; i++) {
111             a[i] = rand.nextInt(size);
112         }
113         insertionSort(a);
114         for(int i=1; i<size; i++) {
115             if (a[i] < a[i-1]) {
116                 System.out.println("ERROR");
117             }
118         }
119     }
120 
121 }
122 


Views
Personal tools