DS Data Structures PPT-module-6

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 13

Data Structure

SUBJECT CODE-ACIC10
(III SEM)
(IARE- UG20)
By
Ms. N.Venkata Sireesha
MODULE –I
INTRODUCTION TO DATA STRUCTURES, SEARCHING AND
SORTING (09) Basic concepts: Introduction to data structures,
classification of data structures, operations on data structures; Algorithm
Specification, Recursive algorithms, Data Abstraction, Performance
analysis- time complexity and space complexity, Asymptotic Notation-Big
O, Omega, and Theta notations. Introduction to Linear and Non Linear data
structures, Searching techniques: Linear and Binary search; Sorting
techniques: Bubble, Selection, Insertion, Quick and Merge Sort and
comparison of sorting algorithms.

3
Quick Sort
Quick sort is a divide and conquer algorithm. Quick sort first divides a
large list into two smaller sub- lists: the low elements and the high
elements. Quick sort can then recursively sort the sub-lists.
The steps are:
1. Pick an element, called a pivot, from the list.
2. Reorder the list so that all elements with values less than the pivot
come before the pivot, while all elements with values greater than
the pivot come after it (equal values can go either way). After this
partitioning, the pivot is in its final position. This is called the
partition operation.
3. Recursively apply the above steps to the sub-list of elements with
smaller values and separately the sub-list of elements with greater
values.
The base case of the recursion is lists of size zero or one, which never
need to be sorted. 4
Quick Sort

5
Quick Sort

6
Quick Sort

7
Quick Sort

8
Quick Sort
Complexity of the Quick Sort Algorithm
To sort an unsorted list with 'n' number of elements, we need to make ((n-1)+(n-2)+(n-3)+......
+1) = (n (n-1))/2 number of comparisions in the worst case. If the list is already sorted, then it
requires 'n' number of comparisions.
Worst Case : O(n2)
Best Case : O (n log n)
Average Case : O (n log n)

9
Quick Sort Program
Function to find the partition position
def partition(array, low, high):

# choose the rightmost element as pivot


pivot = array[high]

# pointer for greater element


i = low - 1

# traverse through all elements


# compare each element with pivot
for j in range(low, high):
if array[j] <= pivot:

# If element smaller than pivot is found


# swap it with the greater element pointed by i
i=i+1
10
Quick Sort
# Swapping element at i with element at j
(array[i], array[j]) = (array[j], array[i])

# Swap the pivot element with the greater element specified by i


(array[i + 1], array[high]) = (array[high], array[i + 1])

# Return the position from where partition is done


return i + 1

# function to perform quicksort

def quickSort(array, low, high):


if low < high:

# Find pivot element such that


# element smaller than pivot are on the left
# element greater than pivot are on the right
pi = partition(array, low, high)

11
Quick Sort
# Recursive call on the left of pivot
quickSort(array, low, pi - 1)

# Recursive call on the right of pivot


quickSort(array, pi + 1, high)

data = [1, 7, 4, 1, 10, 9, -2]


print("Unsorted Array")
print(data)

size = len(data)

quickSort(data, 0, size - 1)

print('Sorted Array in Ascending Order:')


print(data)

12
Quick Sort

Unsorted Array [1, 7, 4, 1, 10, 9, -2]


Sorted Array in Ascending Order: [-2, 1, 1, 4, 7, 9, 10]

13

You might also like