0% found this document useful (0 votes)
7 views3 pages

Sorting Program With Output

The document presents a sorting program that implements three algorithms: Bubble Sort, Merge Sort, and Quick Sort, to sort a list of unsorted roll numbers. It includes the number of comparisons made by each sorting algorithm and displays the sorted results along with their efficiency. The efficiency comparison shows that Merge Sort is the fastest, followed by Quick Sort, while Bubble Sort is the slowest.

Uploaded by

Prathamesh Patil
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)
7 views3 pages

Sorting Program With Output

The document presents a sorting program that implements three algorithms: Bubble Sort, Merge Sort, and Quick Sort, to sort a list of unsorted roll numbers. It includes the number of comparisons made by each sorting algorithm and displays the sorted results along with their efficiency. The efficiency comparison shows that Merge Sort is the fastest, followed by Quick Sort, while Bubble Sort is the slowest.

Uploaded by

Prathamesh Patil
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/ 3

Sorting Program with Output

# List of unsorted roll numbers


roll_numbers = [75, 51, 37, 28, 27, 76, 52, 4, 26, 38, 50, 43, 10, 16, 3, 18, 78, 59,
53, 2, 68, 22, 25, 7, 56, 39, 1, 8, 9, 48, 30, 36, 72, 54, 31, 19, 23,
73, 41, 45, 35, 46, 66, 12, 62, 71, 21, 67, 29, 40, 5, 60, 33, 6, 70,
57, 11, 17, 44, 65]

# Bubble Sort Algorithm


def bubble_sort(arr):
n = len(arr)
comparisons = 0
for i in range(n-1):
for j in range(n-1-i):
comparisons += 1
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
return comparisons

# Merge Sort Algorithm


def merge_sort(arr):
comparisons = [0]
def merge(left, right):
sorted_list = []
i = j = 0
while i < len(left) and j < len(right):
comparisons[0] += 1
if left[i] < right[j]:
sorted_list.append(left[i])
i += 1
else:
sorted_list.append(right[j])
j += 1
sorted_list.extend(left[i:])
sorted_list.extend(right[j:])
return sorted_list

def merge_sort_recursive(arr):
if len(arr) <= 1:
return arr
mid = len(arr) // 2
left = merge_sort_recursive(arr[:mid])
right = merge_sort_recursive(arr[mid:])
return merge(left, right)

sorted_arr = merge_sort_recursive(arr)
return sorted_arr, comparisons[0]

# Quick Sort Algorithm


def quick_sort(arr):
comparisons = [0]
def partition(low, high):
pivot = arr[high]
i = low - 1
for j in range(low, high):
comparisons[0] += 1
if arr[j] < pivot:
i += 1
arr[i], arr[j] = arr[j], arr[i]
arr[i+1], arr[high] = arr[high], arr[i+1]
return i+1

def quick_sort_recursive(low, high):


if low < high:
pi = partition(low, high)
quick_sort_recursive(low, pi - 1)
quick_sort_recursive(pi + 1, high)

quick_sort_recursive(0, len(arr) - 1)
return comparisons[0]

# Sorting and Counting Comparisons


bubble_sorted = roll_numbers[:]
bubble_comparisons = bubble_sort(bubble_sorted)
merge_sorted, merge_comparisons = merge_sort(roll_numbers[:])
quick_sorted = roll_numbers[:]
quick_comparisons = quick_sort(quick_sorted)

# Print Results
print("Unsorted Roll Numbers:", roll_numbers)
print("\nBubble Sort Result:", bubble_sorted, "| Comparisons:", bubble_comparisons)
print("\nMerge Sort Result:", merge_sorted, "| Comparisons:", merge_comparisons)
print("\nQuick Sort Result:", quick_sorted, "| Comparisons:", quick_comparisons)

# Efficiency Comparison
print("\nSorting Efficiency (Lower Comparisons = Better Performance):")
sorting_efficiency = {
"Bubble Sort": bubble_comparisons,
"Merge Sort": merge_comparisons,
"Quick Sort": quick_comparisons
}
sorted_efficiency = sorted(sorting_efficiency.items(), key=lambda x: x[1])
for algo, comps in sorted_efficiency:
print(f"{algo}: {comps} comparisons")

Expected Output

# Expected Output:

Unsorted Roll Numbers: [75, 51, 37, 28, 27, 76, 52, 4, 26, 38, 50, 43, 10, 16, 3, 18, 78, 59,
53, 2, 68, 22, 25, 7, 56, 39, 1, 8, 9, 48, 30, 36, 72, 54, 31, 19, 23,
73, 41, 45, 35, 46, 66, 12, 62, 71, 21, 67, 29, 40, 5, 60, 33, 6, 70,
57, 11, 17, 44, 65]
Bubble Sort Result: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 16, 17, 18, 19, 21, 22, 23, 25, 26,
27, 28, 29, 30, 31, 33, 35, 36, 37, 38, 39, 40, 41, 43, 44, 45, 46, 48, 50,
51, 52, 53, 54, 56, 57, 59, 60, 62, 65, 66, 67, 68, 70, 71, 72, 73, 75, 76, 78]
| Comparisons: ~1830

Merge Sort Result: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 16, 17, 18, 19, 21, 22, 23, 25, 26,
27, 28, 29, 30, 31, 33, 35, 36, 37, 38, 39, 40, 41, 43, 44, 45, 46, 48, 50,
51, 52, 53, 54, 56, 57, 59, 60, 62, 65, 66, 67, 68, 70, 71, 72, 73, 75, 76, 78]
| Comparisons: ~360

Quick Sort Result: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 16, 17, 18, 19, 21, 22, 23, 25, 26,
27, 28, 29, 30, 31, 33, 35, 36, 37, 38, 39, 40, 41, 43, 44, 45, 46, 48, 50,
51, 52, 53, 54, 56, 57, 59, 60, 62, 65, 66, 67, 68, 70, 71, 72, 73, 75, 76, 78]
| Comparisons: ~500

Sorting Efficiency (Lower Comparisons = Better Performance):


1. Merge Sort: ~360 comparisons (Fastest)
2. Quick Sort: ~500 comparisons (Good)
3. Bubble Sort: ~1830 comparisons (Slowest)

You might also like