0% found this document useful (0 votes)
34 views11 pages

DS Unit-1 Algorithms

The document describes algorithms for linear search, binary search, insertion sort, selection sort, bubble sort, quicksort, and mergesort. Linear search has best, average, and worst case time complexities of O(1), O(N), and O(N) respectively, with O(1) space complexity. Binary search has best, average, and worst case time complexities of O(1), O(logN), and O(logN) respectively, with O(1) space complexity for iterative and O(logN) for recursive. The sorting algorithms - insertion sort, selection sort, bubble sort, quicksort, and mergesort - are described through their step-by

Uploaded by

Vyoum . .
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views11 pages

DS Unit-1 Algorithms

The document describes algorithms for linear search, binary search, insertion sort, selection sort, bubble sort, quicksort, and mergesort. Linear search has best, average, and worst case time complexities of O(1), O(N), and O(N) respectively, with O(1) space complexity. Binary search has best, average, and worst case time complexities of O(1), O(logN), and O(logN) respectively, with O(1) space complexity for iterative and O(logN) for recursive. The sorting algorithms - insertion sort, selection sort, bubble sort, quicksort, and mergesort - are described through their step-by

Uploaded by

Vyoum . .
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Linear Search - Algorithm

• Step 1 - Read the search element from the user.


• Step 2 - Compare the search element with the first element in the list.
• Step 3 - If both are matched, then display "Given element is found!!!"
and terminate the function.
• Step 4 - If both are not matched, then compare search element with the
next element in the list.
• Step 5 - Repeat steps 3 and 4 until search element is compared with last
element in the list.
• Step 6 - If last element in the list also doesn't match,
then display "Element is not found!!!" and terminate the function

4/6/2022 5.1 _ Searching 1


Best Case Time Complexity of Linear Search:
O(1)

Average Case Time Complexity of Linear


Search: O(N)

Worst Case Time Complexity of Linear Search:


O(N)

Space Complexity of Linear Search: O(1)


Binary Search - Algorithm
• Step 1 - Read the search element from the user.
• Step 2 - Find the middle element in the sorted list.
• Step 3 - Compare the search element with the middle element in
the sorted list.
• Step 4 - If both are matched, then display "Given element is
found!!!" and terminate the function.
• Step 5 - If both are not matched, then check whether the search
element is smaller or larger than the middle element.
• Step 6 - If the search element is smaller than middle element,
repeat steps 2, 3, 4 and 5 for the left sublist of the middle element.
• Step 7 - If the search element is larger than middle element, repeat
steps 2, 3, 4 and 5 for the right sublist of the middle element.
• Step 8 - Repeat the same process until we find the search element
in the list or until sublist contains only one element.
• Step 9 - If that element also doesn't match with the search
element, then display "Element is not found in the list!!!" and
terminate the function.
4/6/2022 5.1 _ Searching 3
Best Case Time Complexity of Binary Search:
O(1)

Average Case Time Complexity of Binary


Search: O(logN)

Worst Case Time Complexity of Binary Search:


O(logN)

Space Complexity of Binary Search: O(1) for


iterative, O(logN) for recursive.
Insertion Sort - Algorithm
• To sort an array of size n in ascending order:
– 1: Iterate from arr[1] to arr[n] over the array.
– 2: Compare the current element (key) to its
predecessor.
– 3: If the key element is smaller than its
predecessor, compare it to the elements before.
Move the greater elements one position up to
make space for the swapped element.

4/6/2022 5.2 _ Sorting 5


Selection Sort - Algorithm
• Step 1 − Set MIN to location 0
• Step 2 − Search the minimum element in the list
• Step 3 − Swap with value at location MIN
• Step 4 − Increment MIN to point to next
element
• Step 5 − Repeat until list is sorted

4/6/2022 5.2 _ Sorting 6


Bubble Sort - Algorithm

4/6/2022 5.2 _ Sorting 7


Quick Sort – Algorithm(Pivot)
• Step 1 - Consider the first element of the list as pivot (i.e., Element at first
position in the list).
• Step 2 - Define two variables i and j. Set i and j to first and last elements
of the list respectively.
• Step 3 - Increment i until list[i] > pivot then stop.
• Step 4 - Decrement j until list[j] < pivot then stop.
• Step 5 - If i < j then exchange list[i] and list[j].
• Step 6 - Repeat steps 3,4 & 5 until i > j.
• Step 7 - Exchange the pivot element with list[j] element.

4/6/2022 5.2 _ Sorting 8


Merge Sort - Algorithm

4/6/2022 5.2 _ Sorting 9


Time and Space Complexity

4/6/2022 5.2 _ Sorting 10


Thank you

4/6/2022 5.2 _ Sorting 11

You might also like