InsertionSort.java                                                         11/12/2552 17:05

 1   /**
 2    * @(#)InsertionSort.java
 3    *
 4    *
 5    * @¹Ò ¹Ñ¹·ªÑ ¾ÙÅࢵ¡Ô¨
 6    * @50113252042
 7    * @version 1.00 2009/11/12
 8    */
 9    import java.util.Scanner;
10
11   public class InsertionSort {
12
13       /**
14         * Creates a new instance of <code>InsertionSort</code>.
15         */
16       public InsertionSort() {
17       }
18
19       /**
20        * @param args the command line arguments
21        */
22       public static void main(String[] args) {
23           // TODO code application logic here
24           int a[] = {567,345,653,345,467};
25           int temp,count;
26
27           //Input data
28           Scanner scan = new Scanner(System.in);
29           System.out.println("Insertion Sortn===========");
30           System.out.print("Input 5 integer n: ");
31           for (int i = 0; i<5; i++){
32               a[i] = scan.nextInt();
33
34           }
35
36           //Sort data
37           do{
38               count = 0;
39               for (int i = 1; i<5; i++){
40                   for (int j = 0; j<i; j++){
41                       if(a[j]>a[i]){
42                           temp = a[j];
43                           for (int k = j; k<i; k++){
44                               a[k] = a[k+1];
45                           }
46                           a[i] =temp;
47                           count++;
48                       }
49                       System.out.println("nSorting arrey "+i+","+j);
50                   for (int l = 0; l<4; l++){
51                       System.out.print(a[l]+",");
52                   }
53                   System.out.println(a[4]);
54                   }
55
56
57               }
58           } while(count!=0);
59
60
61
62           //Print data
63           System.out.println("nSorted arrey");
64           for (int i = 0; i<4; i++){
65               System.out.print(a[i]+",");
66           }
67           System.out.println(a[4]);
68       }
69   }
70




                                            Page 1 of 1

Insertion Sort Code

  • 1.
    InsertionSort.java 11/12/2552 17:05 1 /** 2 * @(#)InsertionSort.java 3 * 4 * 5 * @¹Ò ¹Ñ¹·ªÑ ¾ÙÅࢵ¡Ô¨ 6 * @50113252042 7 * @version 1.00 2009/11/12 8 */ 9 import java.util.Scanner; 10 11 public class InsertionSort { 12 13 /** 14 * Creates a new instance of <code>InsertionSort</code>. 15 */ 16 public InsertionSort() { 17 } 18 19 /** 20 * @param args the command line arguments 21 */ 22 public static void main(String[] args) { 23 // TODO code application logic here 24 int a[] = {567,345,653,345,467}; 25 int temp,count; 26 27 //Input data 28 Scanner scan = new Scanner(System.in); 29 System.out.println("Insertion Sortn==========="); 30 System.out.print("Input 5 integer n: "); 31 for (int i = 0; i<5; i++){ 32 a[i] = scan.nextInt(); 33 34 } 35 36 //Sort data 37 do{ 38 count = 0; 39 for (int i = 1; i<5; i++){ 40 for (int j = 0; j<i; j++){ 41 if(a[j]>a[i]){ 42 temp = a[j]; 43 for (int k = j; k<i; k++){ 44 a[k] = a[k+1]; 45 } 46 a[i] =temp; 47 count++; 48 } 49 System.out.println("nSorting arrey "+i+","+j); 50 for (int l = 0; l<4; l++){ 51 System.out.print(a[l]+","); 52 } 53 System.out.println(a[4]); 54 } 55 56 57 } 58 } while(count!=0); 59 60 61 62 //Print data 63 System.out.println("nSorted arrey"); 64 for (int i = 0; i<4; i++){ 65 System.out.print(a[i]+","); 66 } 67 System.out.println(a[4]); 68 } 69 } 70 Page 1 of 1