Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 89dcd90

Browse files
committed
update on sorting algorithms
Heap Sort and other sorts missing
1 parent e1bca48 commit 89dcd90

File tree

4 files changed

+176
-1
lines changed

4 files changed

+176
-1
lines changed

DataAndAlgoL/Chpt10SortingAlgorithms/Chpt10Notes.org

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,42 @@
4949
- Every repetition of insertion removes an element from the input data, and inserts it into the correct position in the already sorted list until no input elements remain.
5050
- Sorting is typically done in-place. The resulting array after k iterations has the property where the first k+1 entries are sorted
5151

52-
** SHELL SORT (DIMINISHING INCREMENT SORT)
52+
** SHELL SORT (DIMINISHING INCREMENT SORT)
53+
>> generalization of insertion sort.
54+
*** NOTE -> Insertion sort works efficiently on input that is already almost sorted
55+
>> SHELL SORT AKA *N-gap* insertion sort.
56+
** Instead of comparing only the adjacent pair, shell sort makes several passes and uses various gaps between adjacent elements, ending with the gap of 1 or classical insertion sort
57+
** Variation used in shell sort is to avoid comparing adjacent elements until the last step of the algorithm, meaning last step of the shell sort is insertion sort algorithm
58+
** Fastes of all O(N^2) algorithms
59+
60+
** MERGE SORT
61+
>> Important example of divide and conquer
62+
*** IMPORTANT NOTES:
63+
*merging* -> process of combining 2 sorted files to make one bigger sorted file
64+
*selection* -> process of dividing a file into 2 parts: k smallest element and n - k largest elements
65+
*Selection and Merging* -> 2 opposite operations:
66+
selection splits the list into 2 lists
67+
merging joins 2 list to make 1 list
68+
*Merge Sort -> is Quick Sort's complement* and accesses the data in sequential order
69+
** NOTE -> this algorithm is used for sorting a LINKED LIST
70+
** Insensitive to the initial order of its input
71+
*** NOTE -> Merge sort divides the list into 2 parts, each part is conquered individually.
72+
*** Merge sort starts with the small sublists and finishes with the largest list. As a result does not need a stack
73+
** Stable alfgorithm
74+
75+
** QUICK SORT
76+
** Example of divide and conquer algorithm. AKA Paritition Exchange Sort -> Uses recursive calls for sorting elements
77+
*** NOTE: One of the famous algorithms among comparison-based sorting algorithms
78+
** STEPS:
79+
** DIVIDE: Array[low...high]is partitioned into 2 non-empty subarrays Array[low... pivot] and Array[pivot+1... high]
80+
** Such that each element of Array[low... high] is less than or equal to each element of Array[pivot+1... high]
81+
** PIVOT index is calculated as part of the partitioning procedure
82+
** CONQUER:
83+
** The 2 subarrays Array[low... pivot] and Array[pivot+1... high] are sorted by recursive calls
84+
** ALGORITHM:
85+
*** 1. If there are 1 or no elements in the array to be sorted return.
86+
*** 2. Pick and element in the array to serve as the PIVOT point. (USUALLY THE LEFT MOST ELEMENT IN THE ARRAY IS USED)
87+
*** 3. Split the array into 2 parts- 1 with elements larger than the pivot, other with elements smaller than the PIVOT
88+
*** 4. Recursively repeat the algorithm for both halves of the original array
89+
90+
** HEAP SORT
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package DataAndAlgoL.Chpt10SortingAlgorithms;
2+
3+
import java.util.Arrays;
4+
5+
public class MergeSort {
6+
public static void main(String[] args) {
7+
int[] list ={2,3,2,6,5,1,-2,3,14,12};
8+
mergeSort(list);
9+
System.out.println(Arrays.toString(list));
10+
}
11+
12+
public static void mergeSort(int[] list){
13+
if(list.length > 1){
14+
//merge sort 1st half
15+
int[] firstHalf= new int[list.length/2];
16+
System.arraycopy(list, 0, firstHalf, 0, list.length/2);
17+
mergeSort(firstHalf); //recursive call to merge the first half of the current recursive list
18+
19+
//merge sort 2nd Half
20+
int secondHalfLength= list.length - list.length/2;
21+
int[] secondHalf= new int[secondHalfLength];
22+
System.arraycopy(list, list.length/2, secondHalf, 0, secondHalfLength);
23+
mergeSort(secondHalf); //recursive call to merge the second half of the list of the current recursive list
24+
25+
//merge first half with second half int list (original list)
26+
merge(firstHalf, secondHalf, list);
27+
28+
}
29+
}
30+
31+
public static void merge(int[] list1, int[] list2, int[] temp){
32+
int current1=0; //current index in list 1
33+
int current2=0; //current index in list 2
34+
int current3=0; //current index in list temp
35+
36+
//while both lists have the same size
37+
while(current1 < list1.length && current2 < list2.length){
38+
if(list1[current1] < list2[current2]){
39+
temp[current3++]=list1[current1++];
40+
}else{
41+
temp[current3++]= list2[current2++];
42+
}
43+
}
44+
45+
//if list 1 has larger size than list 2 append the rest
46+
while(current1 < list1.length){
47+
temp[current3++] = list1[current1++];
48+
}
49+
50+
//if list 2 has a larger size than list 1, append the rest
51+
while(current2 < list2.length){
52+
temp[current3++]=list2[current2++];
53+
}
54+
55+
}
56+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package DataAndAlgoL.Chpt10SortingAlgorithms;
2+
3+
public class QuickSort {
4+
public static void main(String[] args) {
5+
6+
}
7+
8+
public static void quickSort(int[] nums, int low, int high){
9+
int pivot;
10+
//termination condition -> where it runs until it reaches stopping point
11+
if(high > low){
12+
pivot= Partition(nums, low, high);
13+
quickSort(nums, low, pivot-1);
14+
quickSort(nums, pivot+1, high);
15+
}
16+
}
17+
18+
public static int Partition(int[] nums, int low, int high){
19+
int left=low;
20+
int right=high;
21+
int pivot_item= nums[low];
22+
23+
while(left < right){
24+
//Move left while item < pivot
25+
while(nums[left]<= pivot_item){
26+
left++;
27+
}
28+
29+
//Move right while item > pivot
30+
while(nums[right] >= pivot_item){
31+
right--;
32+
}
33+
//when index left is higher than index high we swap the elements in left index with right index
34+
if(left < right){
35+
swap(nums, left, right);
36+
}
37+
38+
}
39+
40+
//right is final position for the pivot
41+
nums[low]= nums[high];
42+
nums[right]= pivot_item;
43+
return right;
44+
}
45+
46+
public static void swap(int[] nums, int left, int right){
47+
int temp=0;
48+
temp= nums[left];
49+
nums[left]= nums[right];
50+
nums[right]=temp;
51+
}
52+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package DataAndAlgoL.Chpt10SortingAlgorithms;
2+
3+
public class ShellSort {
4+
public static void main(String[] args) {
5+
6+
}
7+
8+
public static void shellSort(int[] nums){
9+
int i;
10+
int j;
11+
int v;
12+
int h;
13+
14+
for(h=1; h <-nums.length/9; h=3*h+1);
15+
for(; h>0; h=h/3){
16+
for(i= h+1; i<= nums.length; i++){
17+
v= nums[i];
18+
j=i;
19+
while(j > h && nums[j-h]> v){
20+
nums[j]= nums[j-h];
21+
j-=h;
22+
}
23+
24+
nums[j]=v;
25+
}
26+
}
27+
28+
}
29+
}

0 commit comments

Comments
 (0)