Skip to content

Commit fbb44eb

Browse files
committed
Added sorting algorithms insertion and selection sort
1 parent cc740b6 commit fbb44eb

File tree

14 files changed

+62
-106
lines changed

14 files changed

+62
-106
lines changed

bin/com/ncet/cj/Bag.class

-1.23 KB
Binary file not shown.

bin/com/ncet/cj/Dog.class

-511 Bytes
Binary file not shown.

bin/com/ncet/cj/Manager.class

-865 Bytes
Binary file not shown.

bin/com/ncet/cj/Panda.class

-520 Bytes
Binary file not shown.

bin/com/ncet/cj/Product.class

-475 Bytes
Binary file not shown.
1.3 KB
Binary file not shown.
1.33 KB
Binary file not shown.

src/com/ncet/cj/Bag.java

-30
This file was deleted.

src/com/ncet/cj/Dog.java

-14
This file was deleted.

src/com/ncet/cj/Manager.java

-28
This file was deleted.

src/com/ncet/cj/Panda.java

-17
This file was deleted.

src/com/ncet/cj/Product.java

-17
This file was deleted.
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package corejava.algorithms;
2+
3+
import java.util.Arrays;
4+
5+
public class InsertionSort {
6+
7+
public static void main(String[] args) {
8+
int size = 20, count = 0;
9+
int arr[] = new int[size];
10+
for (int i = size - 1; i >= 0; i--)
11+
arr[i] = ++count;
12+
System.out.println("Before Sort :- " + Arrays.toString(arr));
13+
insertionSort(arr, size);
14+
System.out.println("After Sort :- " + Arrays.toString(arr));
15+
}
16+
17+
public static void insertionSort(int[] arr, int size) {
18+
int max, j;
19+
for (int i = 1; i < size; i++) {
20+
max = arr[i];
21+
j = i - 1;
22+
while (j >= 0 && arr[j] > max) {
23+
arr[j + 1] = arr[j];
24+
j -= 1;
25+
}
26+
arr[j + 1] = max;
27+
}
28+
}
29+
30+
}
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package corejava.algorithms;
2+
3+
import java.util.Arrays;
4+
5+
public class SelectionSort {
6+
7+
public static void main(String[] args) {
8+
int size = 10, count = 0;
9+
int arr[] = new int[size];
10+
for (int i = size - 1; i >= 0; i--)
11+
arr[i] = ++count;
12+
System.out.println("Before Sort :- " + Arrays.toString(arr));
13+
selectionSort(arr, size);
14+
System.out.println("After Sort :- " + Arrays.toString(arr));
15+
}
16+
17+
public static void selectionSort(int[] arr, int size) {
18+
int temp, min;
19+
for (int i = 0; i < size - 1; i++) {
20+
min = i;
21+
for (int j = min + 1; j < size; j++) {
22+
if (arr[min] > arr[j]) {
23+
min = j;
24+
}
25+
}
26+
temp = arr[min];
27+
arr[min] = arr[i];
28+
arr[i] = temp;
29+
}
30+
}
31+
32+
}

0 commit comments

Comments
 (0)