Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ GEM
http_parser.rb (~> 0.6.0)
ethon (0.14.0)
ffi (>= 1.15.0)
eventmachine (1.2.7)
eventmachine (1.2.7-x64-mingw32)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dont make changes to this file

execjs (2.8.1)
faraday (1.6.0)
faraday-em_http (~> 1.0)
Expand All @@ -45,7 +45,7 @@ GEM
faraday-net_http_persistent (1.2.0)
faraday-patron (1.0.0)
faraday-rack (1.0.0)
ffi (1.15.3)
ffi (1.15.3-x64-mingw32)
forwardable-extended (2.6.0)
gemoji (3.0.1)
github-pages (218)
Expand Down Expand Up @@ -227,7 +227,7 @@ GEM
jekyll-seo-tag (~> 2.1)
minitest (5.14.4)
multipart-post (2.1.1)
nokogiri (1.12.2-x86_64-linux)
nokogiri (1.12.2-x64-mingw32)
racc (~> 1.4)
octokit (4.21.0)
faraday (>= 0.9)
Expand Down Expand Up @@ -265,12 +265,14 @@ GEM
thread_safe (~> 0.1)
unf (0.1.4)
unf_ext
unf_ext (0.0.7.7)
unf_ext (0.0.7.7-x64-mingw32)
unicode-display_width (1.7.0)
wdm (0.1.1)
zeitwerk (2.4.2)

PLATFORMS
x86_64-linux-musl
x64-mingw32
x86_64-linux

DEPENDENCIES
github-pages
Expand All @@ -279,9 +281,10 @@ DEPENDENCIES
jekyll-paginate
jekyll-seo-tag
jekyll-sitemap
wdm (>= 0.1.0)

RUBY VERSION
ruby 2.7.4p191
ruby 2.6.7p197

BUNDLED WITH
2.2.23
2.2.26
8 changes: 8 additions & 0 deletions _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,14 @@ authors:
twitter: https://twitter.com/
description: "Web Developer"

vivek:
name: Vivek Kalmath
display_name: Vivek Kalmath
gravatar: "/assets/images/avatar/vivek.jpeg"
email: vivek.kalmath2001@gmail.com
web: https://www.linkedin.com/in/vivek-kalmath-1715921bb
twitter: https://twitter.com/VivekKalmath
description: "Problem Solving | Web Developer"

# Reading Files
include:
Expand Down
26 changes: 26 additions & 0 deletions _pages/author-vivek-kalmath.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
title: "vivek"
layout: default
permalink: "/author-vivek-kalmath.html"
---
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="row align-items-center mb-5">
<div class="col-md-9">
<h2 class="font-weight-bold">{{page.title}} <span class="small btn btn-outline-success btn-sm btn-round"><a href="{{ site.authors.vivek.twitter }}">Follow</a></span></h2>
<p><a href="{{ site.authors.vivek.web }}">{{ site.authors.vivek.web }}</a></p>
<p class="excerpt">{{ site.authors.vivek.bio }}</p>
</div>
<div class="col-md-3 text-right">
<img alt="{{ site.authors.vivek.name }}" src="{{ site.authors.vivek.gravatar }}" class="rounded-circle" height="100" width="100">
</div>
</div>
<h4 class="font-weight-bold spanborder"><span>Posts by vivek </span></h4>
{% assign posts = site.posts | where:"author","vivek" %}
{% for post in posts %}
{% include main-loop-card.html %}
{% endfor %}
</div>
</div>
</div>
279 changes: 279 additions & 0 deletions _posts/2021-08-24-sorting-algo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,279 @@
---
layout: post
title: "Sorting Algorithms"
author: vivek
categories: [tutorial]
featured: true
image: assets/images/algo/sorting.jpg
excerpt: "Sorting Algorithms that every programmer should know."
---

Sorting is a technique that is used to arrange the elements in some specific order. Sorting algorithms are the step wise instructions for sorting the list of elements.

### **Why do we use sorting algorithms ?**
Sorting algorithms are used to keep the records in some order, further it would help other algorithms which uses the sorted data as input. Also the sorted list/data is more convenient for both humans and machines to extract the required record.
For example the algorithm Binary Search requires the sorted list of items as input.

There are many type of sorting techniques having different time and space complexity.
Following are some important Sorting Algorithms :
* Bubble Sort
* Insertion Sort
* Selection Sort
* Merge Sort
* Quick Sort
* Heap Sort


## **Bubble Sort**
Bubble sort is an algorithm to sort the list of given elements. It compares all the elements one by one based on their values.
In each iteration the smaller value is placed in beginning of the list, as the bubbles in the water comes up to the surface. Hence it is know as Bubble sort.
Sorting takes place by stepping through all the data items one by one in pairs and comparing adjacent data items and swapping each pair that is out of order.


```c
int arr[] = {6,5,3,1,8,7,2,4}, n=8, i, j, temp;

for(i=0;i<5;i++){
for(j=i+1;j<5;j++){
if(arr[i]>arr[j]){ //swap the elements
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
```

![bubble sort](../assets/images/algo/bubbleSort.gif)

### Complexity analysis of Bubble Sorting

In Bubble Sort, n-1 comparisons will be done in 1st pass, n-2 in 2nd pass, n-3 in 3rd pass and so on. So the total number of comparisons will be
> **(n-1)+(n-2)+(n-3)+.....+2+1
sum = n(n-1)/2**

Hence the complexity of Bubble Sort is O(n<sup>2</sup>).
The main advantage of Bubble Sort is the simplicity of the algorithm.Space complexity for Bubble Sort is O(1), because only single additional memory space is required for temp variable

Best-case Time Complexity will be O(n), it is when the list is already sorted.





## **Insertion Sort**

Insertion Sort is a technique in which an element is selected and placed in its corret position one by one.
We make the given list of elements into sorted and unsorted part. Initially the first element is considered to be sorted.Each element form the unsorted part from left to right is compared with the sorted part and the element is placed in the ordered position.

```c
int arr[] = {6,5,3,1,8,7,2,4}, n=8, i, key, j;

for(i=1; i<n; i++){
key = arr[i];
j = i - 1;
while(j >= 0 && arr[j] > key){
arr[j+1] = arr[j];
j = j - 1;
}
arr[j+1] = key;
}
```

![Insertion sort](../assets/images/algo/insertionSort.gif)

### Complexity analysis of Insertion Sorting
In Insertion Sort , 0 comparisons are made for first element, 1 comparison is made for second element and so on.

> 1 + 2 + ..... + (n-1) = O(n<sup>2</sup>)

##### Big O: best = O(n), worst = O(n<sup>2</sup>), space : O(1)


## **Selection Sort**
In this sorting algorithm we choose the smallest element in the given list and place it in the first place.

Again we select the smallest element of the list except the first element and place it in the second place.
Similarlly we select the minimum element and arrange them in the order. So it is known as Selection Sort.



```c
int arr[] = {5,2,4,6,1,3}, n=6, i, min, j, temp;

for(i = 0; i < n-1; i++){
min = i;
for(j= i+1; j < n; j++)
if(arr[j] < arr[min])
min = j;
temp = arr[min];
arr[min] = arr[i];
arr[i] = temp;
}
```
![Selection sort](../assets/images/algo/selectionSort.gif)

### Complexity analysis of Selection Sorting
Selection sort is inefficient on large lists, and generally performs worse than the similar insertion sort. It has performance advantages over more complicated algorithms in certain situations. It does no more than n swaps, and thus is useful where swapping is very expensive.

##### Big O: best/worst = O(n²), space : O(1)


## **Merge Sort**
Merge Sort follows Divide and Conquer rule.In merge sort the unsorted list is divided into N sublists, each having one element, because a list of one element is considered sorted. Then, it repeatedly merge these sublists, to produce new sorted sublists, and at lasts one sorted list is produced.

Merge Sort is quite fast, and has a time complexity of O(n log n). It is also a stable sort, which means the “equal” elements are ordered in the same order in the sorted list.


```c
int arr = {6,5,3,1,8,7,2,4};

void mergesort(int arr[], int p, int r){
int q;
if(p < r){
q = floor( (p+r) / 2);
mergesort(arr, p, q);
mergesort(arr, q+1, r);
merge(arr, p, q, r);
}
}

void merge(int arr[], int p, int q, int r){
int b[8],i, j, k; //same size of arr[]
k = 0;
i = p;
j = q+1;
while(i <= q && j <= r){
if(arr[i] < arr[j])
b[k++] = arr[i++]; // same as b[k]=arr[i]; k++; i++;
else
b[k++] = arr[j++];
}

while(i <= q)
b[k++] = arr[i++];

while(j = p; i--)
arr[i] = b[--k]; // copying back the sorted list to a[]
}
```
![Merge sort](../assets/images/algo/mergeSort.gif)


### Complexity analysis of Merge Sorting
Time complexity of Merge Sort is O(n Log n) in all 3 cases (worst, average and best) as merge sort always divides the array in two halves and take linear time to merge two halves. It requires equal amount of additional space as the unsorted list. Hence its not at all recommended for searching large unsorted lists.
It is the best Sorting technique for sorting Linked Lists.

##### Big O: best/worst = O(nlogn), space : O(n)


## **Quick Sort**
Quick Sort algorithm also follows the Divide and Conquer rule as Merge Sort. But it is more complex and much faster than Merge sort and rarely reaches its worst case O(n<sup>2</sup>).
It has 3 basic steps:

* Select an element that is designated as the pivot from the array.
* Move smaller elements to the left of the pivot and larger elements to the right of the pivot.
* Using recursion apply steps 1 and 2 to the subsequent sub arrays.


```c
int partition(int arr[],int low,int high)
{
int pivot = arr[high];
int temp;
int i = (low - 1);
for (int j = low; j <= high- 1; j++)
{
if (arr[j] < pivot)
{
i++;
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
temp = arr[i+1];
arr[i+1] = arr[high];
arr[high] = temp;
return (i + 1);
}

void quickSort(int arr[],int low,int high)
{
int pi;
if (low < high)
{
pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
```

![Quick sort](../assets/images/algo/quickSort.gif)


### Complexity analysis of Quick Sorting
The main thing about quick sort is that its worst-case performance is O(N²) which occurs when choosing the first or last element as the pivot. The most important issue in quick sort is thus choosing a good pivot element. This problem has more or less been solved by using the median strategy that guides the performance towards the O(N log N) complexity.

##### Big O: best = O(nlog n), worst = O(n<sup>2</sup>), space : O(log n)

## **Heap Sort**
Heap sort algorithm works using heaps. The max heap is used to select the largest number and placed accordingly. Again we have to rebuild the heap(heapify) and find for next largest element. This process continues till the whole list is sorted. Heap sort is not stable sort.

```c
void heapsort(int a[], int length)
{
buildheap(a, length);
int heapsize, i, temp;
heapsize = length - 1;
for( i=heapsize; i >= 0; i--)
{
temp = a[0];
a[0] = a[heapsize];
a[heapsize] = temp;
heapsize--;
satisfyheap(a, 0, heapsize);
}
}

void buildheap(int a[], int length)
{
int i, heapsize;
heapsize = length - 1;
for( i=(length/2); i >= 0; i--)
satisfyheap(a, i, heapsize);
}

void satisfyheap(int a[], int i, int heapsize)
{
int l, r, largest, temp;
l = 2*i;
r = 2*i + 1;
if(l <= heapsize && a[l] > a[i])
largest = l;
else
largest = i;
if( r <= heapsize && a[r] > a[largest])
largest = r;
if(largest != i)
{
temp = a[i];
a[i] = a[largest];
a[largest] = temp;
satisfyheap(a, largest, heapsize);
}
}
```

![Heap sort](../assets/images/algo/heapSort.gif)


### Complexity analysis of Heap Sorting
Using the heap, finding the next largest element takes O(log N) time, instead of O(N) for a linear scan as in simple selection sort. This allows heap sort to run in O(N log N) time, and this is also the worst case complexity.

##### Big O: O(nlog n), space : O(1)



Every Sorting Algorithm have its own significance. So we must select the sorting algorithm according to the requirement of the data we have. The suitable sorting algorithm is the one that takes less time and space for the given set of data items. We can save lots of time and memory by selecting the correct algorithm.
Binary file added assets/images/algo/bubbleSort.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/images/algo/heapSort.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/images/algo/insertionSort.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/images/algo/mergeSort.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/images/algo/quickSort.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/images/algo/selectionSort.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/images/algo/sorting.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/images/avatar/vivek.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.