Skip to content
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 19 commits into from
Nov 27, 2020
3 changes: 3 additions & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
- [Sorting Algorithms](./SortingAlgorithms/README.md)
- [Quick Sort](./SortingAlgorithms/QuickSort.md)
- [Radix Sort](./SortingAlgorithms/RadixSort.md)
- [Insertion Sort](./SortingAlgorithms/InsertionSort/insertion.md)
- [Merge Sort](./SortingAlgorithms/MergeSort/merge.md)
- [Selection Sort](./SortingAlgorithms/SelectionSort/selection.md)
- [Backtracking](./Backtracking/Backtracking.md)
- [Pseudocode](./Backtracking/Pseudocode/Pseudocode.md)
- [Problems](./Backtracking/Problems/Problems.md)
Expand Down
86 changes: 86 additions & 0 deletions src/SortingAlgorithms/InsertionSort/insertion.md
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
Copy link

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


```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;
}
}

}
```


72 changes: 72 additions & 0 deletions src/SortingAlgorithms/MergeSort/merge.md
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>

5 changes: 4 additions & 1 deletion src/SortingAlgorithms/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ A Sorting Algorithm is used to rearrange a given array or list elements accordin
## Topics Covered

- [Quick Sort](./QuickSort.md)
- [Radix Sort](./RadixSort.md)
- [Radix Sort](./RadixSort.md)
- [Insertion Sort](./InsertionSort/insertion.md)
- [Merge Sort](./MergeSort/merge.md)
- [SelectionSort](./SelectionSort/selection.md)
36 changes: 36 additions & 0 deletions src/SortingAlgorithms/SelectionSort/selection.md
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>