Syllabus Introduction – Basic terminology – Data structures – Data structure operations - ADT – Algorithms: Complexity, Time – Space trade off - Mathematical notations and functions - Asymptotic notations – Linear and Binary search - Bubble sort - Insertion sort
https://www.cs.usfca.edu/~galles/visualization/Search.html (Final) for linear search and binary search https://blog.penjee.com/binary-vs-linear-search-animated-gifs/ (for linear search and binary search comparisons) https://www.cs.usfca.edu/~galles/visualization/Algorithms.html http://www.cs.armstrong.edu/liang/animation/web/BubbleSort.html (Bubble Sort) http://www.ee.ryerson.ca/~courses/coe428/sorting/bubblesort.html ( bUBBLE SORT) https://courses.cs.vt.edu/csonline/Algorithms/Lessons/InsertionCardSort/insertioncardsort.swf (Insertion sort final) http://www.ee.ryerson.ca/~courses/coe428/sorting/insertionsort.html (INSERTION SORT ADDITIONAL)
SORTING: BUBBLE SORT Let A be a list of n numbers. Sorting A refers to the operation og rearranging the elements of A so that they are in increasing order: A[1]<A[2]<A[3]<………….A[N]
Bubble Sort Algorithm for( i =0; i <n; i ++) { for(j=0; j<n-i-1; j++) { if( a[j] > a[j+1]) temp = a[j]; a[j] = a[j+1]; a[j+1] = temp; } }
Bubble Sort Example
Time Comlexity Total number of comparison required to sort the array: =(N-1)+(N-2)+(N-3)+…2+1 =(N*(N-1))/2 So worst case complexity is O(N 2) HIGHEST ORDER IS N 2
Complexities
CONT.. Best Case Time Complexity:O (N) The best case occurs when the array is already sorted. Number f comparison required: N-1 Number of swap=0
Insertion Sort Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is considered an ” in-place ” sorting algorithm, meaning it doesn’t require any additional memory space beyond the original array.
Insertion Algorithm Steps S tart with the second element of the array as the first element in the array is assumed to be sorted. Compare the second element with the first element, check if the second element is smaller, and swap them. Move to the third element and compare it with the second element, then the first element and swap as necessary to put it in the correct position among the first three elements. Continue this process, comparing each element with the ones before it and swapping as needed to place it in the correct position among the sorted elements. Repeat until the entire array is sorted.
Insertion Sort algorithm Sort(int a[],int n) { int i , j, key; for( i =1; i <n; i ++) { key = a[ i ]; j = i-1; while(j>=0 && key < a[j]) { a[j+1] = a[j]; j--; } a[j+1] = key; } }
Work for you…. You have been given a pack of cards (Only Spades) numbered 1 to 13 (Ace, 2 to 10, Jack Queen, King) in random order. Write a program to sort these cards. Use Bubble sort and print each line of output after every iteration. For the same above problem, Write the program using Insertion sort and print each line of output after every iteration. Compare the number of steps for each sorting technique