├── README.md ├── sorting.py └── sorting_personal_notes.pdf /README.md: -------------------------------------------------------------------------------- 1 | # Visualize_DS 2 | This repo focuses on visualizing the data structures and algorithms 3 | 4 | ## Sorting Visualization 5 | 6 | There are various types of sorting algorithms and it is very difficult to understand their working without visualization. 7 | Hence we decided to visualize these sorting algos in python with the help of matplotlib.animations 8 | 9 | Pro tip :- Time complexity can also be seen through these visualizations 10 | 11 | #### All sortings are done on randomly generated arrays of size 30. 12 | 13 | ### Bubble Sort 14 | Total operations = 435 15 | 16 | ![bubble](https://user-images.githubusercontent.com/46081301/79339497-9f94f880-7f46-11ea-9c7f-5059d65c87d2.gif) 17 | 18 | ### Selection Sort 19 | Total operations = 460 20 | 21 | ![selection](https://user-images.githubusercontent.com/46081301/79339506-a3287f80-7f46-11ea-8dd1-fac20ec0e092.gif) 22 | 23 | ### Insertion Sort 24 | Total operations = 230 25 | 26 | ![Insertion](https://user-images.githubusercontent.com/46081301/79339499-a15ebc00-7f46-11ea-9348-8a86af034180.gif) 27 | 28 | ### Quick Sort 29 | Total operations = 131 30 | 31 | ![quick](https://user-images.githubusercontent.com/46081301/79339503-a28fe900-7f46-11ea-9394-137c927c13f4.gif) 32 | 33 | ### Merge Sort 34 | Total operations = 177 35 | 36 | ![merge](https://user-images.githubusercontent.com/46081301/79339502-a1f75280-7f46-11ea-8d21-d86ad6e607d6.gif) 37 | -------------------------------------------------------------------------------- /sorting.py: -------------------------------------------------------------------------------- 1 | import random 2 | import matplotlib.pyplot as plt 3 | import matplotlib.animation as anim 4 | 5 | 6 | def swap(A, i, j): 7 | a = A[j] 8 | A[j] = A[i] 9 | A[i] = a 10 | # also in python A[i],A[j]=A[j],A[i] 11 | 12 | 13 | def sort_buble(arr): 14 | if (len(arr) == 1): 15 | return 16 | for i in range(len(arr) - 1): 17 | for j in range(len(arr) - 1 - i): 18 | if (arr[j] > arr[j + 1]): 19 | swap(arr, j, j + 1) 20 | yield arr 21 | 22 | def insertion_sort(arr): 23 | if(len(arr)==1): 24 | return 25 | for i in range(1,len(arr)): 26 | j = i 27 | while(j>0 and arr[j-1]>arr[j]): 28 | swap(arr,j,j-1) 29 | j-=1 30 | yield arr 31 | 32 | def quick_Sort(arr,p,q): 33 | if(p>=q): 34 | return 35 | piv = arr[q] 36 | pivindx = p 37 | for i in range(p,q): 38 | if(arr[i]mid): 81 | while(j<=ub): 82 | new.append(arr[j]) 83 | j+=1 84 | else: 85 | while(i<=mid): 86 | new.append(arr[i]) 87 | i+=1 88 | for i,val in enumerate(new): 89 | arr[lb+i] = val 90 | yield arr 91 | 92 | def heapify(arr,n,i): 93 | largest = i 94 | l = i*2+1 95 | r = i*2+2 96 | while(larr[largest]): 97 | largest = l 98 | while(rarr[largest]): 99 | largest = r 100 | if(largest!=i): 101 | swap(arr,i,largest) 102 | yield arr 103 | yield from heapify(arr,n,largest) 104 | 105 | def heap_sort(arr): 106 | n = len(arr) 107 | for i in range(n,-1,-1): 108 | yield from heapify(arr,n,i) 109 | for i in range(n-1,0,-1): 110 | swap(arr,0,i) 111 | yield arr 112 | yield from heapify(arr,i,0) 113 | 114 | def shell_sort(arr): 115 | sublistcount = len(arr) // 2 116 | while sublistcount > 0: 117 | for start_position in range(sublistcount): 118 | yield from gap_InsertionSort(arr, start_position, sublistcount) 119 | sublistcount = sublistcount // 2 120 | 121 | def gap_InsertionSort(nlist,start,gap): 122 | for i in range(start+gap,len(nlist),gap): 123 | 124 | current_value = nlist[i] 125 | position = i 126 | 127 | while position>=gap and nlist[position-gap]>current_value: 128 | nlist[position]=nlist[position-gap] 129 | position = position-gap 130 | yield nlist 131 | 132 | nlist[position]=current_value 133 | yield nlist 134 | 135 | def count_sort(arr): 136 | max_val = max(arr) 137 | m = max_val + 1 138 | count = [0] * m 139 | 140 | for a in arr: 141 | count[a] += 1 142 | yield arr 143 | i = 0 144 | for a in range(m): 145 | for c in range(count[a]): 146 | arr[i] = a 147 | i += 1 148 | yield arr 149 | yield arr 150 | 151 | 152 | 153 | 154 | 155 | n = int(input("Enter the number of elements:")) 156 | al = int(input("Choose algorithm: 1.Bubble \n 2.Insertion \n 3.Quick \n 4.Selection \n 5.Merge Sort \n 6.Heapify \n 7.Shell \n 8.Count sort")) 157 | array = [i + 1 for i in range(n)] 158 | random.shuffle(array) 159 | 160 | if(al==1): 161 | title = "Bubble Sort" 162 | algo = sort_buble(array) 163 | elif(al==2): 164 | title = "Insertion Sort" 165 | algo = insertion_sort(array) 166 | elif(al==3): 167 | title = "Quick Sort" 168 | algo = quick_Sort(array,0,n-1) 169 | elif(al==4): 170 | title="Selection Sort" 171 | algo = selection_sort(array) 172 | elif (al == 5): 173 | title = "Merge Sort" 174 | algo=merge_sort(array,0,n-1) 175 | elif (al == 6): 176 | title = "Heap Sort" 177 | algo = heap_sort(array) 178 | elif (al == 7): 179 | title = "Shell Sort" 180 | algo = shell_sort(array) 181 | elif (al == 8): 182 | title = "Count Sort" 183 | algo = count_sort(array) 184 | # Initialize fig 185 | fig, ax = plt.subplots() 186 | ax.set_title(title) 187 | 188 | bar_rec = ax.bar(range(len(array)), array, align='edge') 189 | 190 | ax.set_xlim(0, n) 191 | ax.set_ylim(0, int(n * 1.1)) 192 | 193 | text = ax.text(0.02, 0.95, "", transform=ax.transAxes) 194 | 195 | epochs = [0] 196 | 197 | 198 | def update_plot(array, rec, epochs): 199 | for rec, val in zip(rec, array): 200 | rec.set_height(val) 201 | epochs[0]+= 1 202 | text.set_text("No.of operations :{}".format(epochs[0])) 203 | 204 | 205 | anima = anim.FuncAnimation(fig, func=update_plot, fargs=(bar_rec, epochs), frames=algo, interval=1, repeat=False) 206 | plt.show() 207 | -------------------------------------------------------------------------------- /sorting_personal_notes.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PushkaraSharma/Visualize_DS/a8356a65af29b3e36a454a5473b95e32e0f82073/sorting_personal_notes.pdf --------------------------------------------------------------------------------