forked from codeIIEST/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fresh pull request for sorting algorithms #26
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
9faf0bb
Add selection sort
Ahel2000 c6d35cc
Create selection.md
Ahel2000 4cc29f2
Add merge sort
Ahel2000 b1da6ea
Create merge.md
Ahel2000 7e5eb85
Add files via upload
Ahel2000 d9ef8cd
Create insertion.md
Ahel2000 cd24067
Update README.md
Ahel2000 3743f84
Update SUMMARY.md
Ahel2000 56bdd26
Update SUMMARY.md
Ahel2000 68be0b3
Delete Selection.java
Ahel2000 a550957
Delete Merge.java
Ahel2000 7ac2a85
Delete Insertion.java
Ahel2000 178f08c
Update selection.md
Ahel2000 d706f20
Update merge.md
Ahel2000 2271cca
Update insertion.md
Ahel2000 1c7f1ba
Update SUMMARY.md
Ahel2000 4b3cfe3
Update insertion.md
Ahel2000 348b3fc
Update insertion.md
Ahel2000 b447fa8
Merge branch 'main' into main
sadn1ck File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# Insertion Sort | ||
|
||
## Example Problem | ||
|
||
Given an array ``arr = [a1,a2,a3,a4,a5...,an] `` , we need to sort the array the array with the help of Insertion Sort algorithm. | ||
|
||
<hr> | ||
|
||
## Discussion | ||
|
||
Look at the following list of numbers: | ||
``` | ||
36 10 5 23 56 12 | ||
``` | ||
We scan the array from left to right. For every element in the array, we must ensure that the elements to the left are less than the current element( for sorting in ascending order) and the elements to the right have not yet been seen. | ||
|
||
We take 2 pointers- ``i,j``. We exchange ``arr[i]`` with each larger element to its left. At first, the pointer i is at position 0. So ``arr[0]=36`` is at its correct position since there is there no element to its left. | ||
|
||
We then increment the pointer ''i''. The pointer i now points to ``arr[1]=10``. Now the element to the left of 10 is 36. We see that the element 10 is not in its correct position, since there is an element to the left(=36) which is greater than 10. So we exchange their positions. The present array is ``10 36``. | ||
|
||
We increment the pointer to point at ``arr[2]=5``. The element 5 is not at its correct position. So we exchange 5 with 36. The current array is ``10 5 36``. The element 5 is still not in its correct position as 10 is greater than 5. So we exchange 10 with 5. The current array becomes ``5 10 36``. | ||
|
||
We continue this way. | ||
``` | ||
Note: | marks the position of j. | ||
When i=0: | ||
36 | 10 5 23 56 12 | ||
|
||
When i=1: | ||
36 10 | 5 23 56 12 | ||
//36 greater than 10. So we exchange. | ||
10 | 36 5 23 56 12 | ||
//Go no further as there is no element to left. | ||
|
||
When i=2: | ||
10 36 5 | 23 56 12 | ||
//36 grrater than 5. So we exchange. | ||
10 5 | 36 23 56 12 | ||
//10 greater than 5 . So we exchange. | ||
5 | 10 36 23 56 12 | ||
//5 is at its correct position. | ||
|
||
When i=3: | ||
5 10 36 23 | 56 12 | ||
//We exchange 36 and 23 as 36>23. | ||
5 10 23 | 36 56 12 | ||
//10<23. So we need not go any further. | ||
|
||
When i=4: | ||
5 10 23 36 56 | 12 | ||
//56 is larger than every element to the left. So no exchange needed. | ||
|
||
When i=5: | ||
5 10 23 36 56 12 | | ||
//12 is smaller than 56. So we exchange. | ||
5 10 23 36 12 | 56 | ||
//12 is smaller than 36. So we exchange. | ||
5 10 23 12 | 36 56 | ||
//12 is smaller than 23. So exchange is needed. | ||
5 10 12 | 23 36 56 | ||
//12 is greater than 10. Hence we stop. | ||
``` | ||
At this position we stop since we have traversed through the entire array. | ||
|
||
|
||
<hr> | ||
|
||
## Code for the Algorithm | ||
|
||
|
||
### Code For Insertion Sort | ||
|
||
```java | ||
public void sort(Comparable []a){ | ||
for(int i=0;i<a.length;i++){ | ||
for (int j = i; j > 0; j--){ | ||
if (less(a[j], a[j-1]))//if element at j is less than the element at j-1 | ||
exch(a, j, j-1); //we exchange the two elements. | ||
else break; | ||
} | ||
} | ||
|
||
} | ||
``` | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# Merge Sort | ||
|
||
## Example Problem | ||
|
||
Given an array ``arr = [a1,a2,a3,a4,a5...,an] `` , we need to sort the array the array with the help of Merge Sort algorithm. | ||
<hr> | ||
|
||
## Discussion | ||
|
||
Merge sort is an application of the Divide and Conquer Principle.The basic approach is to: | ||
- Divide the array into two halves | ||
- Sort each half recursively | ||
- Merge the two halves | ||
<p> | ||
<img src="https://user-images.githubusercontent.com/66062504/99971682-89e08f00-2dc3-11eb-9860-973c837e0e6f.png"/> | ||
</p> | ||
|
||
<hr> | ||
|
||
### Code For Merge Sort | ||
|
||
Merge Sort: | ||
```java | ||
public static void mergesort(Comparable[] workplace,int lowerbound,int upperbound){ | ||
if(lowerbound==upperbound){ | ||
return; | ||
} | ||
else{ | ||
int mid=(lowerbound+upperbound)/2; | ||
mergesort(workplace,lowerbound,mid); | ||
mergesort(workplace,mid+1,upperbound); | ||
merge(workplace,lowerbound,mid+1,upperbound); | ||
} | ||
} | ||
|
||
``` | ||
Merging two sorted arrays: | ||
```java | ||
public static void merge(Comparable[] workplace,int lowPtr,int highPtr,int upperbound) | ||
{ | ||
int low=lowPtr; | ||
int mid=highPtr-1; | ||
int j=0; | ||
int n=upperbound-low+1; | ||
while(low<=mid && highPtr<=upperbound){ | ||
if(theArray[low].compareTo(theArray[highPtr])<0){ | ||
workplace[j++]=theArray[low++]; | ||
}else{ | ||
workplace[j++]=theArray[highPtr++]; | ||
} | ||
} | ||
|
||
while(low<=mid){ | ||
workplace[j++]=theArray[low++]; | ||
} | ||
|
||
while(highPtr<=upperbound){ | ||
workplace[j++]=theArray[highPtr++]; | ||
} | ||
|
||
for(j=0;j<n;j++){ | ||
theArray[lowPtr+j]=workplace[j]; | ||
} | ||
} | ||
``` | ||
|
||
### Time Complexity: | ||
Time complexity of Merge Sort is O(n*Log n) in all the 3 cases (worst, average and best) as merge sort always divides the array in two halves and takes linear time to merge two halves. It requires equal amount of additional space as the unsorted array. | ||
|
||
### Resources: | ||
<a href="https://www.geeksforgeeks.org/merge-sort">GeeksforGeeks</a> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Selection Sort | ||
|
||
## Example Problem | ||
|
||
Given an array ``arr = [a1,a2,a3,a4,a5...,an] `` , we need to sort the array the array with the help of Selection Sort algorithm. | ||
|
||
<hr> | ||
|
||
## Discussion | ||
|
||
Selection sort is a simple sorting algorithm where we divide an array into two parts- sorted and unsorted. Initially the sorted part is empty. We find the smallest element of the unsorted part. Then we swap it with the leftmost element of the unsorted part. The leftmost element then becomes a member of the sorted part. | ||
<p> | ||
<img src="https://user-images.githubusercontent.com/66062504/99956054-614c9b00-2dab-11eb-81d1-c53c51fd776b.jpeg" alt="selection"/> | ||
</p> | ||
|
||
<hr> | ||
|
||
### Code For Selection Sort | ||
```java | ||
public static void sort(Comparable a[]) | ||
{ | ||
// initialise instance variables | ||
for(int i=0;i<a.length;i++){ | ||
int min=i; | ||
for(int j=i+1;j<a.length;j++){ | ||
if(less(a[j],a[min])){//if a[j] less than min | ||
min=j; //then update position of min | ||
} | ||
} | ||
exch(a,i,min);//swap the i-th element with the minimum element | ||
} | ||
} | ||
|
||
``` | ||
### Resources: | ||
<a href="https://www.hackerearth.com/practice/algorithms/sorting/selection-sort/tutorial/">HackerEarth</a> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add some space here, markdown shenanigans