0% found this document useful (0 votes)
11 views6 pages

PRACTICAL NO

Uploaded by

rb7880219
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)
11 views6 pages

PRACTICAL NO

Uploaded by

rb7880219
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/ 6

PRACTICAL NO-4

OBJECT:WAP for Merge Sort.


Theory:
Merge sort is a sorting algorithm that uses a divide-and-conquer strategy to
organize a list of elements in ascending order:
1. Divide: Split the list into smaller sublists until each sublist contains only
one element
2. Conquer: Recursively solve the subproblems
3. Solve: Combine the solutions of the subproblems to solve the original
problem
Merge sort is considered one of the most efficient sorting algorithms. It was
invented by John von Neumann in 1945.
Here are some more details about merge sort:
• Execution: Merge sort can be executed in two ways: top-down or
bottom-up.
• Stability: Most implementations of merge sort produce a stable sort,
which means that the relative order of equal elements is the same in the
input and output.
• Time complexity: The time complexity of merge sort is O(nlogn).
• Space complexity: The space complexity of merge sort is O(n).

Algorithms:
1. MERGE_SORT(arr, beg, end)
2. if beg < end
3. set mid = (beg + end)/2
4. MERGE_SORT(arr, beg, mid)
5. MERGE_SORT(arr, mid + 1, end)
6. MERGE (arr, beg, mid, end)

7. end of if
8. END MERGE_SORT
Programs:
def merge_sort(arr):
# If the array has more than 1 element
if len(arr) > 1:
mid = len(arr) // 2 # Find the middle of the list
left_half = arr[:mid] # Divide the elements into two halves
right_half = arr[mid:]

print(f"Dividing: {arr} -> Left: {left_half}, Right: {right_half}")

merge_sort(left_half) # Recursively sort the left half


merge_sort(right_half) # Recursively sort the right half

# Now merge the two halves


i=j=k=0

# Merge the temp arrays back into the original array arr
while i < len(left_half) and j < len(right_half):
if left_half[i] < right_half[j]:
arr[k] = left_half[i]
i += 1
else:
arr[k] = right_half[j]
j += 1
k += 1

# Check if any element was left in left_half


while i < len(left_half):
arr[k] = left_half[i]
i += 1
k += 1

# Check if any element was left in right_half


while j < len(right_half):
arr[k] = right_half[j]
j += 1
k += 1

print(f"Merging: {arr}")

def main():
# Take input from the user
arr = list(map(int, input("Enter the list of numbers (space-separated): ").split()))
print(f"Original array: {arr}"
# Perform Merge Sort
merge_sort(arr)
print(f"Sorted array: {arr}")
if _name_ == "_main_":
main()
Output:
PRACTICAL NO-5
OBJECT:WAP for Quick Sort.
Theory:
Quick sort is a sorting algorithm that uses a divide-and-conquer strategy to sort an array. It
works by:
• Selecting a pivot: Choosing a value in the array to be the pivot element
• Partitioning: Ordering the remaining elements so that values smaller than the pivot
are on the left and values larger than the pivot are on the right
• Repeating: Recursively applying these steps to the sub-arrays on either side of the
pivot until the sub-arrays are too small to be sorted
Quick sort is considered one of the most efficient sorting algorithms, along with Merge
Sort. It's useful for sorting large data sets.
The complexity of the Quick Sort algorithm is:
• Average case: O(n*log(n))
• Best case: O(n*log(n))
• Worst case: O(n^2)
• Space complexity: O(n log n)

Algoritms:
1. QUICKSORT (array A, start, end)
2. {
3. 1 if (start < end)
4. 2 {
5. 3 p = partition(A, start, end)
6. 4 QUICKSORT (A, start, p - 1)
7. 5 QUICKSORT (A, p + 1, end)
8. 6 }
9. }
Programs:
def quick_sort(arr, low, high):
if low < high:
# Partitioning index
pi = partition(arr, low, high)

# Print the array after partitioning


print(f"Partitioning: {arr} with pivot index {pi}")

# Recursively sort the two subarrays


quick_sort(arr, low, pi - 1)
quick_sort(arr, pi + 1, high)

def partition(arr, low, high):


# Pivot is the last element
pivot = arr[high]
i = low - 1 # Index of smaller element

# Rearrange the array so that elements smaller than the pivot are on the left
# and elements greater than the pivot are on the right
for j in range(low, high):
if arr[j] < pivot:
i += 1
arr[i], arr[j] = arr[j], arr[i] # Swap

# Swap the pivot with the element at i + 1


arr[i + 1], arr[high] = arr[high], arr[i + 1]

# Return the partition index


return i + 1

def main():
# Take input from the user
arr = list(map(int, input("Enter the list of numbers (space-separated): ").split()))
print(f"Original array: {arr}")

# Perform Quick Sort


quick_sort(arr, 0, len(arr) - 1)

print(f"Sorted array: {arr}")

if _name_ == "_main_":
main()

Output:

You might also like