DSA Arrays (1)
DSA Arrays (1)
AND ALGORITHMS -
ARRAYS
Dr. Muhammad Awais Sattar
Assistant Professor RSCI
1
ARRAYS/LISTS
An array is a data structure used to store multiple
elements.
Arrays are used by many algorithms.
For example, An algorithm can be used to look through
an array to find the lowest value
In Python, an array can be created like this:
my_array = [7, 12, 9, 4, 11]
ARRAYS/LISTS
Arrays are indexed, meaning that each element in the
array has an index, a number that says where in the
array the element is located.
Python use zero-based indexing for arrays, meaning that
the first element in an array can be accessed at index 0.
OR
ALGORITHM: FIND THE LOWEST
VALUE IN AN ARRAY
In the image above, first value 7 is copied, then values 11 and 12 are
shifted one place up in the array, and at last value 7 is put where
value 11 was before.
The number of shifting operations is reduced from 12 to 2 in this case.
INSERTION SORT
my_array = [64, 34, 25, 12, 22, 11, 90, 5]
n = len(my_array)
for i in range(1,n):
insert_index = i
current_value = my_array[i]
for j in range(i-1, -1, -1):
if my_array[j] > current_value:
my_array[j+1] = my_array[j]
insert_index = j
else:
break
my_array[insert_index] = current_value
print("Sorted array:", my_array)
QUICK SORT
As the name suggests, Quicksort is one of the fastest
sorting algorithms.
The Quicksort algorithm takes an array of values,
chooses one of the values as the 'pivot' element, and
moves the other values so that lower values are on the
left of the pivot element, and higher values are on the
right of it.
The last element of the array is chosen to be the pivot
element, but we could also have chosen the first
element of the array, or any element in the array really.
QUICK SORT
How it works:
1. Choose a value in the array to be the pivot element.
2. Order the rest of the array so that lower values than the
pivot element are on the left, and higher values are on the
right.
3. Swap the pivot element with the first element of the higher
values so that the pivot element lands in between the lower
and higher values.
4. Do the same operations (recursively) for the sub-arrays on
the left and right side of the pivot element.
QUICK SORT
Manual Run Through
Step 1: We start with an unsorted array.
[ 11, 9, 12, 7, 3]
Step 2: We choose the last value 3 as the pivot element.
[ 11, 9, 12, 7, 3]
Step 3: The rest of the values in the array are all greater
than 3, and must be on the right side of 3. Swap 3 with 11.
[ 3, 9, 12, 7, 11]
QUICK SORT
Step 4: Value 3 is now in the correct position. We need to sort
the values to the right of 3. We choose the last value 11 as the
new pivot element.
[ 3, 9, 12, 7, 11]
Step 5: The value 7 must be to the left of pivot value 11, and 12
must be to the right of it. Move 7 and 12.
[ 3, 9, 7, 12, 11]
Step 6: Swap 11 with 12 so that lower values 9 anf 7 are on the
left side of 11, and 12 is on the right side.
[ 3, 9, 7, 11, 12]
QUICK SORT
Step 7: 11 and 12 are in the correct positions. We choose 7 as the pivot element
in sub-array [ 9, 7], to the left of 11.
[ 3, 9, 7, 11, 12]
Step 8: We must swap 9 with 7.
[ 3, 7, 9, 11, 12]
And now, the array is sorted.
We have already seen that last value of the array is chosen as the pivot
element, and the rest of the values are arranged so that the values lower than
the pivot value are to the left, and the higher values are to the right.
After that, the pivot element is swapped with the first of the higher values. This
splits the original array in two, with the pivot element in between the lower and
the higher values.
QUICKSORT
To implement the Quicksort algorithm in a programming
language, we need:
An array with values to sort.
A quickSort method that calls itself (recursion) if the sub-
array has a size larger than 1.
A partition method that receives a sub-array, moves
values around, swaps the pivot element into the sub-
array and returns the index where the next split in sub-
arrays happens
QUICK SORT
def quick_sort(arr):
# Base case: If the array has one or zero
elements, it is already sorted
if len(arr) <= 1:
return arr
# Choose the pivot element (middle element
in this case)
pivot = arr[len(arr) // 2]
QUICK SORT
# Create sub-arrays:
# Example usage
# left: elements less than the pivot
arr = [10, 7, 8, 9, 1, 5] # Input
# middle: elements equal to the pivot array
sorted_arr = quick_sort(arr) # Sort
# right: elements greater than the pivot
the array using quick_sort function
left = [x for x in arr if x < pivot] print("Sorted array:", sorted_arr) #
Output the sorted array
middle = [x for x in arr if x == pivot]
right = [x for x in arr if x > pivot]
return -1