13
Coding - Data Structures & Algorithms Table of Content -------------------------------------------------- Time Complexity 2 Time Complexities of Data Structures 2 Time Complexities of Sorting Algorithms 3 Time Complexity Chart 4 Time Complexity :: Common Data Structures Algorithms 4 Time Complexity :: Array Sorting Algorithms 6 Sort 7 Bubble Sort 7 Code 8 Selection Sort 9 Code 9 Insertion Sort 10 Code 10 Merge Sort 11 Code 12 Coding - Algorithm and Data Structures 1 of 13

Coding - Data Structures & Algorithmskotikaranki.com/wp-content/uploads/2019/09/Sorting-algorithms-and... · Coding - Data Structures & Algorithms Table of Content ----- Time Complexity

  • Upload
    others

  • View
    19

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Coding - Data Structures & Algorithmskotikaranki.com/wp-content/uploads/2019/09/Sorting-algorithms-and... · Coding - Data Structures & Algorithms Table of Content ----- Time Complexity

Coding - Data Structures & Algorithms 

 

Table of Content -------------------------------------------------- 

  

Time Complexity 2 

Time Complexities of Data Structures 2 

Time Complexities of Sorting Algorithms 3 

Time Complexity Chart 4 

Time Complexity :: Common Data Structures Algorithms 4 

Time Complexity :: Array Sorting Algorithms 6 

Sort 7 

Bubble Sort 7 

Code 8 

Selection Sort 9 

Code 9 

Insertion Sort 10 

Code 10 

Merge Sort 11 

Code 12 

 

 

 

   

Coding - Algorithm and Data Structures 1 of 13 

Page 2: Coding - Data Structures & Algorithmskotikaranki.com/wp-content/uploads/2019/09/Sorting-algorithms-and... · Coding - Data Structures & Algorithms Table of Content ----- Time Complexity

Coding - Data Structures & Algorithms 

-------------------------------------------------- 

Time Complexity  

Time Complexities of Data Structures 

 

____________________________________________________________________________  

 

Coding - Algorithm and Data Structures 2 of 13 

Page 3: Coding - Data Structures & Algorithmskotikaranki.com/wp-content/uploads/2019/09/Sorting-algorithms-and... · Coding - Data Structures & Algorithms Table of Content ----- Time Complexity

Time Complexities of Sorting Algorithms 

 

 

Coding - Algorithm and Data Structures 3 of 13 

Page 4: Coding - Data Structures & Algorithmskotikaranki.com/wp-content/uploads/2019/09/Sorting-algorithms-and... · Coding - Data Structures & Algorithms Table of Content ----- Time Complexity

Time Complexity Chart 

  

 

 

Coding - Algorithm and Data Structures 4 of 13 

Page 5: Coding - Data Structures & Algorithmskotikaranki.com/wp-content/uploads/2019/09/Sorting-algorithms-and... · Coding - Data Structures & Algorithms Table of Content ----- Time Complexity

Time Complexity :: Common Data Structures Algorithms 

 

 

   

Coding - Algorithm and Data Structures 5 of 13 

Page 6: Coding - Data Structures & Algorithmskotikaranki.com/wp-content/uploads/2019/09/Sorting-algorithms-and... · Coding - Data Structures & Algorithms Table of Content ----- Time Complexity

Time Complexity :: Array Sorting Algorithms 

 

   

Coding - Algorithm and Data Structures 6 of 13 

Page 7: Coding - Data Structures & Algorithmskotikaranki.com/wp-content/uploads/2019/09/Sorting-algorithms-and... · Coding - Data Structures & Algorithms Table of Content ----- Time Complexity

Sort  The ideal sorting algorithm would have the following properties:  Stable: Equal keys aren’t reordered. Operates in place, requiring O(1) extra space. Worst-case O(n·lg(n)) key comparisons. Worst-case O(n) swaps. Adaptive: Speeds up to O(n) when data is nearly sorted or when there are few unique keys.  There is no algorithm that has all of these properties, and so the choice of sorting algorithm depends on the application. 

  

Bubble Sort 

Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order.  Example: 

First Pass: 

( 5 1 4 2 8 ) –> ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps since 5                                                   

> 1. 

( 1 5 4 2 8 ) –> ( 1 4 5 2 8 ), Swap since 5 > 4 

( 1 4 5 2 8 ) –> ( 1 4 2 5 8 ), Swap since 5 > 2 

( 1 4 2 5 8 ) –> ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5), algorithm                                                     

does not swap them. 

Second Pass: 

( 1 4 2 5 8 ) –> ( 1 4 2 5 8 ) 

( 1 4 2 5 8 ) –> ( 1 2 4 5 8 ), Swap since 4 > 2 

Coding - Algorithm and Data Structures 7 of 13 

Page 8: Coding - Data Structures & Algorithmskotikaranki.com/wp-content/uploads/2019/09/Sorting-algorithms-and... · Coding - Data Structures & Algorithms Table of Content ----- Time Complexity

( 1 2 4 5 8 ) –> ( 1 2 4 5 8 ) 

( 1 2 4 5 8 ) –> ( 1 2 4 5 8 ) 

Now, the array is already sorted, but our algorithm does not know if it is completed. The                                 

algorithm needs one whole pass without any swap to know it is sorted. 

Third Pass: 

( 1 2 4 5 8 ) –> ( 1 2 4 5 8 ) 

( 1 2 4 5 8 ) –> ( 1 2 4 5 8 ) 

( 1 2 4 5 8 ) –> ( 1 2 4 5 8 ) 

( 1 2 4 5 8 ) –> ( 1 2 4 5 8 ) 

Code 

 

public static void bubbleSort(int []a) {     boolean swapped = true;    for(int i = a.length - 1; i >= 0 && swapped; i--) {     swapped = false;    for(int j = 0; j < i; j++) {     if(a[j] > a[j+1]) {  int tmp = a[j];  a[j] = a[j+1];  a[j+1] = tmp;  swapped = true;    }  }  }  } 

 ____________________________________________________________________________ 

 

Coding - Algorithm and Data Structures 8 of 13 

Page 9: Coding - Data Structures & Algorithmskotikaranki.com/wp-content/uploads/2019/09/Sorting-algorithms-and... · Coding - Data Structures & Algorithms Table of Content ----- Time Complexity

 

Selection Sort 

The selection sort algorithm sorts an array by repeatedly finding the minimum element (considering ascending order) from unsorted part and putting it at the beginning. The algorithm maintains two subarrays in a given array.  1) The subarray which is already sorted. 2) Remaining subarray which is unsorted.  In every iteration of selection sort, the minimum element (considering ascending order) from the unsorted subarray is picked and moved to the sorted subarray.  

Code 

 

public static void selectionSort(int []a) {     for(int i = 0; i < a.length; i++) {     int idx = i;     for(int j = i + 1; j < a.length; j++) {  if(a[j] < a[idx]) {  idx = j;  }    }     if(idx != i) {    int tmp = a[idx];  a[idx] = a[i];  a[i] = tmp;  }  }   } 

 ____________________________________________________________________________ 

 

Coding - Algorithm and Data Structures 9 of 13 

Page 10: Coding - Data Structures & Algorithmskotikaranki.com/wp-content/uploads/2019/09/Sorting-algorithms-and... · Coding - Data Structures & Algorithms Table of Content ----- Time Complexity

Insertion Sort 

Insertion sort is a simple sorting algorithm that works the way we sort playing cards in our hands. 

 

 

Code 

public static void insertionSort(int a[]) {  for (int i = 0; i < a.length; i++) {  for (int j = i; j > 0; j--) {  if (a[j] < a[j-1]) {  int tmp = a[j];  a[j] = a[j - 1];  a[j - 1] = tmp;  } else {  break;  }  }  }  } 

 ____________________________________________________________________ 

Coding - Algorithm and Data Structures 10 of 13 

Page 11: Coding - Data Structures & Algorithmskotikaranki.com/wp-content/uploads/2019/09/Sorting-algorithms-and... · Coding - Data Structures & Algorithms Table of Content ----- Time Complexity

  

Merge Sort 

Merge Sort is a Divide and Conquer algorithm. It divides input array in two halves, calls itself for the two halves and then merges the two sorted halves. The merge() function is used for merging two halves. The merge(arr, l, m, r) is key process that assumes that arr[l..m] and arr[m+1..r] are sorted and merges the two sorted sub-arrays into one.  The following diagram from wikipedia shows the complete merge sort process for an example array {38, 27, 43, 3, 9, 82, 10}. If we take a closer look at the diagram, we can see that the array is recursively divided in two halves till the size becomes 1. Once the size becomes 1, the merge processes comes into action and starts merging arrays back till the complete array is merged.   

  

Coding - Algorithm and Data Structures 11 of 13 

Page 12: Coding - Data Structures & Algorithmskotikaranki.com/wp-content/uploads/2019/09/Sorting-algorithms-and... · Coding - Data Structures & Algorithms Table of Content ----- Time Complexity

Code 

public static void sort(int[] a, int start, int end) {   if (start >= end)  return;   int mid = (end + start) / 2;  sort(a, start, mid);  sort(a, mid + 1, end);  merge(a, start, end);  }  //------------------------------------------------  private static void merge(int[] a, int start, int end) {   int LEFTEND = (end + start) / 2;  int RIGHTSTART = LEFTEND + 1;   int LEFTIDX = start;  int RIGHTIDX = RIGHTSTART;   int SIZE = end - start + 1;  int tmp[] = new int[SIZE];  int TMPIDX = 0;   while (LEFTIDX <= LEFTEND && RIGHTIDX <= end) {   if (a[LEFTIDX] <= a[RIGHTIDX]) {  tmp[TMPIDX++] = a[LEFTIDX++];  } else {  tmp[TMPIDX++] = a[RIGHTIDX++];  }  }   //copy back left side left overs if any  while (LEFTIDX <= LEFTEND) {  tmp[TMPIDX++] = a[LEFTIDX++];  } 

Coding - Algorithm and Data Structures 12 of 13 

Page 13: Coding - Data Structures & Algorithmskotikaranki.com/wp-content/uploads/2019/09/Sorting-algorithms-and... · Coding - Data Structures & Algorithms Table of Content ----- Time Complexity

  //copy back irhg side left overs if any  while (RIGHTIDX <= end) {  tmp[TMPIDX++] = a[RIGHTIDX++];  }  //copy back the temp to original array  LEFTIDX = start;  for (int i = 0; i < SIZE; i++) {  a[LEFTIDX++] = tmp[i];  }   } 

   

____________________________________________________________________________ 

Coding - Algorithm and Data Structures 13 of 13