Skip to main content

Posts

Showing posts with the label DataStructure

Quick Sort Algorithm

  QuickSort is a Divide and Conquer algorithm. It picks an element as a pivot and partitions the given array around the picked pivot. Java public class Sort{ public static void main (String []args){ int [] arr = { 3 , 5 , 1 , 6 , 2 , 4 , 8 }; quickSort(arr, 0 , arr. length - 1 ); for ( int x:arr) System. out . print (x+ " " ); } public static int partition ( int arr[], int low, int high){ int pivot = arr[high]; int i = (low- 1 ); for ( int j=low; j<high; j++){ if (arr[j] <= pivot){ i++; int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } int temp = arr[i+ 1 ]; arr[i+ 1 ] = arr[high]; arr[high] = temp; return i+ 1 ; } public static void quickSort ( ...

Insertion Sort Algorithm

The array is virtually split into a sorted and an unsorted part. Values from the unsorted part is picked and placed at the correct position in the sorted part. Complexity:- Worst-case and average-case:-  O ( n 2  ) best case:- O(n) Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 public class InsertionSort{ public static void main (String []args){ int [] arr = { 3 , 5 , 1 , 6 , 2 , 4 }; for ( int i= 1 ; i<arr. length ; i++){ int temp = arr[i]; int j = i - 1 ; while (j>= 0 && arr[j]>temp){ arr[j+ 1 ] = arr[j]; j--; } arr[j+ 1 ] = temp; } for ( int x:arr) System. out . print (x+ " " ); } } Python def insertion_sort(arra...

Binary Search Algorithm

Binary search compares the target value to the middle element of the array.  If they are not equal, the half in which the target cannot lie is eliminated and the search continues on the remaining half,  again taking the middle element to compare to the target value, and repeating this until the target value is found.  If the search ends with the remaining half being empty, the target is not in the array. Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 public class BinarySearch{ public static void main (String []args){ int [] arr = { 1 , 2 , 4 , 5 , 7 , 11 , 12 , 15 }; int target = 7 ; System. out . println (binarySearchRec(arr, target, 0 , arr. length - 1 )); System. out . println (binarySearchIter(arr, target)); } // Using recursion public static boolean binarySearchRec ( int [...

Linear Search Algorithm

It is the simplest searching algorithm. A sequential search is made over all items one by one. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 public class Serach{ public static void main (String[] args){ int [] arr = { 3 , 2 , 5 , 4 , 7 , 9 , 1 }; int target = 9 ; System. out . println (linearSearch(arr,target)); } public static String linearSearch ( int [] arr, int target){ for ( int i= 0 ; i<arr. length - 1 ; i++){ if (arr[i]==target) return "found at index " +i; } return "not found" ; } }

Bubble Sort Algorithm

 Bubble sort is one of the simplest sorting algorithms.    It is a comparison-based sorting algorithm.  In this sorting algorithm, each pair of adjacent elements is compared and swapped if they are not in order.  Disadvantage:-   Not suitable for large data sets. Average and worst-case complexity = O(n^2) Normal code:- 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 public class BubbleSort{ public static void main (String[] args){ int [] arr = { 3 , 2 , 5 , 4 , 7 , 9 , 1 }; for ( int i= 0 ; i<arr. length - 1 ; i++){ for ( int j= 0 ; j<arr. length - 1 -i; j++){ if (arr[j]>arr[j+ 1 ]){ int temp = arr[j]; arr[j] = arr[j+ 1 ]; arr[j+ 1 ] = temp; } } } for ( int x:arr) System. out . print (x+ " " ); } } Optim...