Skip to content

Commit 587b886

Browse files
committed
Sorting Programs
1 parent aa6d53a commit 587b886

File tree

5 files changed

+157
-25
lines changed

5 files changed

+157
-25
lines changed

InterviewPrograms/src/com/algorithm/sorting/BubbleSort.java

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,51 @@
22

33
import java.util.Arrays;
44

5+
/*
6+
* Bubble Sort
7+
* ------------
8+
* Bubble sort, sometimes referred to as sinking sort,
9+
* is a simple sorting algorithm that repeatedly steps
10+
* through the list, compares adjacent elements and
11+
* swaps them if they are in the wrong order.
12+
*
13+
* The pass through the list is repeated until the
14+
* list is sorted. The algorithm, which is a comparison
15+
* sort, is named for the way smaller or larger
16+
* elements "bubble" to the top of the list.
17+
*
18+
* Time Complexity
19+
* ----------------
20+
* Worst Case : O(n^2)
21+
* Best Case : O(n)
22+
*/
523
public class BubbleSort {
6-
724
public static void main(String[] args) {
825
int array[] ={3,60,35,2,45,320,5};
9-
System.out.println("Before Sorting :: "+Arrays.toString(array));
26+
System.out.println("Before Sorting :: ");
27+
System.out.println(Arrays.toString(array));
1028
bubbleSort(array);
11-
System.out.println("After Sorting :: "+Arrays.toString(array));
29+
System.out.println("After Sorting :: ");
30+
System.out.println(Arrays.toString(array));
1231
}
1332

1433
public static void bubbleSort(int array[]){
1534
int n = array.length;
16-
for(int i=0;i<n-1;i++)
17-
for(int j=0;j<n-i-1;j++)
35+
for(int i=0;i<n-1;i++){
36+
for(int j=0;j<n-i-1;j++){
1837
if(array[j] > array[j+1]){
1938
int temp = array[j];
2039
array[j] = array[j+1];
2140
array[j+1] = temp;
2241
}
42+
}
43+
}
2344
}
24-
2545
}
46+
/*
47+
OUTPUT
48+
Before Sorting ::
49+
[3, 60, 35, 2, 45, 320, 5]
50+
After Sorting ::
51+
[2, 3, 5, 35, 45, 60, 320]
52+
*/

InterviewPrograms/src/com/algorithm/sorting/InsertionSort.java

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,32 @@
11
package com.algorithm.sorting;
22

33
import java.util.Arrays;
4-
4+
/*
5+
* Insertion Sort
6+
* --------------
7+
* Insertion sort is the sorting mechanism where
8+
* the sorted array is built having one item at a time.
9+
* The array elements are compared with each
10+
* other sequentially and then arranged simultaneously
11+
* in some particular order.
12+
*
13+
* It is much less efficient on large lists than
14+
* more advanced algorithms such as quicksort, heapsort,
15+
* or merge sort.
16+
*
17+
* Time Complexity
18+
* ---------------
19+
* Worst Case : O(n^2)
20+
* Best Case : O(n)
21+
*/
522
public class InsertionSort {
623
public static void main(String[] args) {
724
int array[] ={3,60,35,2,45,320,5};
8-
System.out.println("Before Sorting :: "+Arrays.toString(array));
9-
insertionSort(array);
10-
System.out.println("After Sorting :: "+Arrays.toString(array));
25+
System.out.println("Before Sorting :: ");
26+
System.out.println(Arrays.toString(array));
27+
insertionSort(array);
28+
System.out.println("After Sorting :: ");
29+
System.out.println(Arrays.toString(array));
1130
}
1231

1332
public static void insertionSort(int array[]){
@@ -22,4 +41,11 @@ public static void insertionSort(int array[]){
2241
array[j+1] = key;
2342
}
2443
}
25-
}
44+
}
45+
/*
46+
OUTPUT
47+
Before Sorting ::
48+
[3, 60, 35, 2, 45, 320, 5]
49+
After Sorting ::
50+
[2, 3, 5, 35, 45, 60, 320]
51+
*/

InterviewPrograms/src/com/algorithm/sorting/MergeSort.java

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,30 @@
22

33
import java.util.Arrays;
44

5+
/*
6+
* Merge Sort
7+
* ----------
8+
* In computer science, merge sort is an efficient, general-purpose,
9+
* comparison-based sorting algorithm.
10+
* Merge sort is a divide and conquer algorithm.
11+
*
12+
* It divides input array in two halves, calls itself for
13+
* the two halves and then merges the two sorted halves.
14+
*
15+
*
16+
* Time Complexity
17+
* ---------------
18+
* Worst Case : O(n*log(n))
19+
* Best Case : O(n*log(n))
20+
*/
521
public class MergeSort {
622
public static void main(String[] args) {
723
int array[] = {90,23,101,45,65,23,67,89,34,23};
8-
System.out.println("Before Sorting :: "+Arrays.toString(array));
24+
System.out.println("Before Sorting :: ");
25+
System.out.println(Arrays.toString(array));
926
sort(array,0,array.length-1);
10-
System.out.println("After Sorting :: "+Arrays.toString(array));
27+
System.out.println("After Sorting :: ");
28+
System.out.println(Arrays.toString(array));
1129
}
1230

1331
public static void mergeSort(int array[], int leftIndex, int midIndex, int rightIndex) {
@@ -55,4 +73,12 @@ public static void sort(int array[], int leftIndex, int rightIndex) {
5573
mergeSort(array, leftIndex, midIndex, rightIndex);
5674
}
5775
}
58-
}
76+
}
77+
78+
/*
79+
OUTPUT
80+
Before Sorting ::
81+
[90, 23, 101, 45, 65, 23, 67, 89, 34, 23]
82+
After Sorting ::
83+
[23, 23, 23, 34, 45, 65, 67, 89, 90, 101]
84+
*/

InterviewPrograms/src/com/algorithm/sorting/QuickSort.java

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,35 @@
11
package com.algorithm.sorting;
22

33
import java.util.Arrays;
4-
4+
/*
5+
* Quick Sort
6+
* ----------
7+
* Quicksort (partition-exchange sort) is an efficient
8+
* sorting algorithm.
9+
*
10+
* Quicksort is a divide-and-conquer algorithm.
11+
* It works by selecting a 'pivot' element from the
12+
* array and partitioning the other elements into
13+
* two sub-arrays, according to whether they are
14+
* less than or greater than the pivot.
15+
* The sub-arrays are then sorted recursively.
16+
* This can be done in-place, requiring small additional
17+
* amounts of memory to perform the sorting.
18+
*
19+
* Time Complexity
20+
* ---------------
21+
* Worst Case : O(n^2)
22+
* Best Case : O(n*log(n))
23+
*
24+
*/
525
public class QuickSort {
6-
726
public static void main(String[] args) {
827
int array[] = {90,23,101,45,65,23,67,89,34,23};
9-
System.out.println("Before Sorting :: "+Arrays.toString(array));
28+
System.out.println("Before Sorting :: ");
29+
System.out.println(Arrays.toString(array));
1030
sort(array,0,array.length-1);
11-
System.out.println("After Sorting :: "+Arrays.toString(array));
31+
System.out.println("After Sorting :: ");
32+
System.out.println(Arrays.toString(array));
1233
}
1334

1435
public static void sort(int array[], int low, int high) {
@@ -35,4 +56,11 @@ public static int partition(int array[], int low, int high) {
3556
array[high] = temp;
3657
return i + 1;
3758
}
38-
}
59+
}
60+
/*
61+
OUTPUT
62+
Before Sorting ::
63+
[90, 23, 101, 45, 65, 23, 67, 89, 34, 23]
64+
After Sorting ::
65+
[23, 23, 23, 34, 45, 65, 67, 89, 90, 101]
66+
*/

InterviewPrograms/src/com/algorithm/sorting/SelectionSort.java

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,31 @@
22

33
import java.util.Arrays;
44

5+
/*
6+
* Selection Sort
7+
* --------------
8+
* The selection sort algorithm sorts an array
9+
* by repeatedly finding the minimum element
10+
* (considering ascending order) from unsorted
11+
* part and putting it at the beginning.
12+
* The algorithm maintains two subarrays in a given array.
13+
* - The subarray which is already sorted.
14+
* - Remaining subarray which is unsorted.
15+
*
16+
* Time Complexity
17+
* ---------------
18+
* Worst Case : O(n^2)
19+
* Best Case : O(n*log(n))
20+
*
21+
*/
522
public class SelectionSort {
6-
723
public static void main(String[] args) {
8-
int array[] = {64,25,12,22,11};
9-
System.out.println("Before Sorting :: "+Arrays.toString(array));
24+
int array[] = {64,25,12,22,11};
25+
System.out.println("Before Sorting :: ");
26+
System.out.println(Arrays.toString(array));
1027
selectionSort(array);
11-
System.out.println("After Sorting :: "+Arrays.toString(array));
28+
System.out.println("After Sorting :: ");
29+
System.out.println(Arrays.toString(array));
1230
}
1331

1432
public static void selectionSort(int array[]) {
@@ -19,10 +37,17 @@ public static void selectionSort(int array[]) {
1937
for (int j = i + 1; j < n; j++)
2038
if (array[j] < array[minIndex])
2139
minIndex = j;
40+
//swap the minimum elt with the elt in index i.
2241
int temp = array[minIndex];
2342
array[minIndex] = array[i];
2443
array[i] = temp;
2544
}
2645
}
27-
28-
}
46+
}
47+
/*
48+
OUTPUT
49+
Before Sorting ::
50+
[64, 25, 12, 22, 11]
51+
After Sorting ::
52+
[11, 12, 22, 25, 64]
53+
*/

0 commit comments

Comments
 (0)