Data Structures and Algorithms-1
Data Structures and Algorithms-1
Algorithms
Linked
Array Queue Stack Trees Graphs
List
Designed by: Murku
Algorithms
• Benefits
• Language free
• Helps in Analysis
• Formulates formula to solve a particular type of problem
• A Data structures needs set of algorithms to structure the data into memory.
• There are multiple operations that are needed to be performed on our data
structure i.e.
A B C D E F G
0 1 2 3 4 5 6
Arr A B C D E F G
• Arr[i]
0 1 2 3 4 5 6
Arr A B C D E F G
Traverse(arr[], n)
1. Begin for( int i=0; i<n; i++)
2. Set i 0 {
3. if i = n then go to step 6 System.out.println(arr[i]);
4. Print array element on index i }
5. i=i+1 go to step 3
6. End
Designed by: Murku
Searching an element from array
• Two Methods
• Linear Search
• Binary Search
0 1 2 3 4 5 6
Arr A B C D E F G
0 1 2 3 4 5 6
S=Mid+1 4
E=6
Arr A B C D E F G Mid=(S+E)/2 5
S= 4
Arr[Mid]=‘E’ ? Arr[Mid]=‘E’ ? Arr[Mid]=‘E’ ? E=Mid-1 4
Mid=(S+E)/2 4
Arr[Mid]>‘E’? NO Arr[Mid]>‘E’? YES
Arr[Mid]<‘E’? YES Arr[Mid]<‘E’? NO
Bubble_Sort(arr[],n)
1. Begin
2. Set i 0
3. If i = n then go to step 11
4. Set flag 0
5. Set j 0
6. If j < n-i go to step 9
Time Complexity 7. If element in at index j is greater then element at index j+1, then swap elements and set flag
Worst Case: O(N2) 1
8. J=j+1 go to step 6
Average Case: O(N2) 9. If flag = 0 go to step 11
Best Case: O(N) 10. i=i+1 go to step 3
11. end
Insertion_Sort(arr[],n)
1. Begin
2. Set i 1
3. If i = n then go to step 11
4. Set temp array element at ith index
5. Set j i-1
Time Complexity 6. While j>=0 and array element at index j > temp repeat steps 7 and 8
Worst Case: O(N2) 7. Set array at index j+1to array element at index j
Average Case: O(N2) 8. j=j-1 go to step 6
9. Set array element at j+1 to temp
Best Case: O(N) 10. i=i+1 go to step 3
11. end
Merge(arr[], s, mid, e)
Merge Sort 1.
2.
Begin
Set n1 mid-s+1
3. Set n2 e – mid
4. Set LA[] size to n1
5. Set RA[] size to n2
• Algorithm 6. Set i 0, set j 0
7. If i = n1 go to step 10
8. Set LA[i] to arr[s+i]
9. i=i+1 go to step 7
Merge_Sort(arr[],s,e) 10. If j=n2 go to step 13
1. Begin 11.
12.
Set RA[j] to arr[mid+1+j]
J=j+1 go to step 10
2. If s > e go to step 7 13. Set i,j 0 and k s
14. While i<n1 and j<n2 repeat steps 15 and 16
3. Set mid (s+e)/2 15. If LA[i] < RA[j] then set arr[k] to LA[i] and increment i by 1,.. ELSE
set arr[k] to RA[j] and increment j by 1
4. Merge_Sort(arr[], s, mid) 16. k=k+1
17. While i < n1 repeat step 18
5. Merge_Sort(arr[], mid+1, e) 18. Set arr[k] to LA[i] and increment i and k by 1
6. Merge(arr[], s, mid, e) 19. While j < n2 repeat step 20
20. Set arr[k] to RA[j] and increment j and k by 1
7. end 21. end
Designed by: Murku
• Algorithm
Quick sort
Partition(arr[], s, e)
• Algorithm 1. Begin
2. Set p arr[s], set i s and j e
Quick_Sort(arr[], s, e) 3. While i < j repeat steps 4 to 8
1. Begin 4. Set i i+1
2. If s > e go to step 6 5. If arr[i] < p then go to step 4
3. Set j Partition(arr[], s,e) 6. Set j j-1
4. Quick_Sort(s, j) 7. if arr[j] > p then go to step 6
5. Quick_Sort(j+1, e) 8. If i < j swap a[i] and a[j]
6. end 9. Swap arr[s] to arr[j]
10. Return j
Designed by: Murku
Count Sort
• Algorithm
Count_Sort(arr[],r,n)
1. Set i 0
2. Set count[] size to r+1
3. If i = n go to step 6
4. Count[arr[i]]++
Time Complexity 5. i = i+1 go to step 3
Worst Case: O(N+k) 6. Set i = 1
Average Case: O(N+K) 7. If j = r, go to step 10
Best Case: O(N+K) 8. Count[j]=count[j]+count[j-1]
9. j=j+1, go to step 7
10. Set k 0
11. If k = n go to step 15
12. Out[count[arr[k]]-1]=arr[k]
13. count[arr[k]]—
14. K++ go to step 11
15. end
Designed by: Murku
Radix Sort
• Algorithm
• Algorithm Count_Sort(arr[],d,n,r)
1. Set i 0
Radix_Sort(Arr[], n) 2. Set count[] size to r+1
1. Begin 3. If i = n go to step 6
4. Count[(arr[i]/d)%10]++
2. Find max element and assign to 5. i = i+1 go to step 3
max 6. Set i = 1
7. If j = r, go to step 10
3. Set d 1 8. Count[j]=count[j]+count[j-1]
9. j=j+1, go to step 7
4. If max/d > 0 go to step 7
10. Set k 0
5. Count_Sort(Arr[], d, n,r) 11. If k = n go to step 15
12. Out[count[(arr[k]/d)%10]-1]=arr[k]
6. d = d*10 go to step 4 13. count[(arr[k]/d)%10] - -
7. end 14. K++ go to step 11
15. end
Designed by: Murku
Queues
0 1 2 3 4 5 6
Arr A B C D E F G
Rear
Front
0 1 2 3 4 5 6
Arr A B C D E F G
Front
Rear
0 1 2 3 4 5 6
Arr A B C D E F G
Top
0 1 2 3 4 5 6
Arr A B C D E F G
Top
Head
Head
Head
3 2 1 9
In Oder : 56,9,3,8,2,24,1
9 24
56 3 2 1
9 24
56
3
Designed by: Murku
Converting Simple Binary Tree To
Binary Search Tree
8
1. Perform In order traversal
store result in an array 9
24
In Oder : 56,9,3,8,2,24,1
1 3 9 56
Designed by: Murku
Searching in BST
• Search(root,elem)
1. Begin
2. if root = null print element not found and go to step 6
3. if root.data = elem then print “Element Found” and go to step 6
4. if root.data < elem then call Search(root.left, elem)
5. If root.data > elem then call Search(root.right, elem)
6. end