Implement Insertion sort algorithm in java.

LAB # 7

INSERTION AND SELECTION SORT IN JAVA

 

LAB TASK

OBJECTIVE # 1

Implement Insertion sort algorithm in java.

SOURCE CODE

public class Lab7_1 {

public static void main(String[] args) {

int[] list = {-1000,77,33,44,22,28,11,55};

System.out.println(“Insertion sort”);

for(int k = 1; k < list.length; k++){

int temp = list[k];

int ptr = k – 1;

while(temp < list[ptr]){

list[ptr+1] = list[ptr];

ptr = ptr – 1;

}// end while loop

list[ptr+1] = temp;

for(int t = 1; t < list.length; t++){

System.out.print(list[t]+”, “);

}

System.out.println();

}// end for loop

}// end main method

}// end class

 

OUTPUT

Insertion sort

77, 33, 44, 22, 28, 11, 55,

33, 77, 44, 22, 28, 11, 55,

33, 44, 77, 22, 28, 11, 55,

22, 33, 44, 77, 28, 11, 55,

22, 28, 33, 44, 77, 11, 55,

11, 22, 28, 33, 44, 77, 55,

11, 22, 28, 33, 44, 55, 77,

 

 

 

 

OBJECTIVE # 2

Implement selection sort algorithm in java.

SOURCE CODE

public class lab7_2 {

public static void main(String[] args) {

int[] list = {10000,33,44,22,88,11,55};

System.out.println(“Selection sort”);

for(int n = 0; n < list.length; n++){

int loc = min(list, n, list.length);

int temp = list[n];

list[n] = list[loc];

list[loc] = temp;

for(int t = 0; t < list.length; t++){

System.out.print(list[t]+”, “);

}// end inner for loop

System.out.println();

}// end for loop

}// end main method

static int min(int[] a, int k, int n){

int m = a[k];

int l = k;

for(int j = k + 1; j < n; j++){

if(m > a[j]){

m = a[j];

l = j;

}// end if

}// end for loop

return l;

}// end minimum method

}// end class

OUTPUT

Selection sort

11, 33, 44, 22, 88, 10000, 55,

11, 22, 44, 33, 88, 10000, 55,

11, 22, 33, 44, 88, 10000, 55,

11, 22, 33, 44, 88, 10000, 55,

11, 22, 33, 44, 55, 10000, 88,

11, 22, 33, 44, 55, 88, 10000,

11, 22, 33, 44, 55, 88, 10000,

 

 

 

 

 

 

OBJECTIVE # 3

Implement both algorithms of task 1 and task 2 for string.

SOURCE CODE

 

public class lab7_3 {

public static void main(String[] args) {

String[] list = {“Umair”, “Usman”, “kamran”, “Rind”, “Amir”};

System.out.print(“STRING ARRAY : “);

for(int k = 0; k < list.length; k++){

System.out.print(list[k]+”, “);

}// end for loop

insertion_Sort(list);

selection_Sort(list);

}// end main method

public static void insertion_Sort(String[] a){

String[] i_Sort = new String[a.length+1];

i_Sort[0] = “-1”;

for(int k = 1; k < i_Sort.length; k++){

i_Sort[k] = a[k-1];

}// end for loop

System.out.println(“\n\nInsertion sort”);

for(int j = 1; j < i_Sort.length; j++){

String temp = i_Sort[j];

int ptr = j – 1;

while(temp.compareToIgnoreCase(i_Sort[ptr]) < 0){

i_Sort[ptr+1] = i_Sort[ptr];

ptr = ptr – 1;

}// end while loop

i_Sort[ptr+1] = temp;

for(int t = 1; t < i_Sort.length; t++){

System.out.print(i_Sort[t]+”, “);

}

System.out.println();

}// end for loop

}// end insertion_Sort method

public static void selection_Sort(String[] b){

System.out.println(“\n\nSelection sort”);

 

for(int n = 0; n < b.length; n++){

int loc = min(b, n, b.length);

String temp = b[n];

b[n] = b[loc];

b[loc] = temp;

 

for(int t = 0; t < b.length; t++){

System.out.print(b[t]+”, “);

}// end inner for loop

System.out.println();

}// end for loop

 

}// end selection_Sort method

 

static int min(String[] s, int k, int n){

String m = s[k];

int l = k;

 

for(int j = k + 1; j < n; j++){

if(m.compareToIgnoreCase(s[j]) > 0){

m = s[j];

l = j;

}// end if

}// end for loop

 

return l;

}// end minimum method

 

}// end class

 

OUTPUT

 

 

STRING ARRAY : Umair, Usman, kamran, Rind, Amir,

 

Insertion sort

Umair, Usman, kamran, Rind, Amir,

Umair, Usman, kamran, Rind, Amir,

kamran, Umair, Usman, Rind, Amir,

kamran, Rind, Umair, Usman, Amir,

Amir, kamran, Rind, Umair, Usman,

 

 

Selection sort

Amir, Usman, kamran, Rind, Umair,

Amir, kamran, Usman, Rind, Umair,

Amir, kamran, Rind, Usman, Umair,

Amir, kamran, Rind, Umair, Usman,

Amir, kamran, Rind, Umair, Usman,

 

 

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

three × 1 =