Skip to content

Commit 5e3943d

Browse files
committed
updated .py filename
1 parent 0dbb276 commit 5e3943d

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
def merge_sort(arr):
2+
"""
3+
Function to perform Merge Sort on a given list.
4+
5+
Args:
6+
arr (list): Unsorted list to be sorted.
7+
8+
Returns:
9+
list: Sorted list in ascending order.
10+
"""
11+
if len(arr) > 1:
12+
# Finding the middle of the array to divide it
13+
mid = len(arr) // 2
14+
15+
# Dividing the array into two halves
16+
left_half = arr[:mid]
17+
right_half = arr[mid:]
18+
19+
# Recursively sorting both halves
20+
merge_sort(left_half)
21+
merge_sort(right_half)
22+
23+
# Initializing pointers for left, right, and the merged array
24+
i = j = k = 0
25+
26+
# Merging the two sorted halves into a single sorted array
27+
while i < len(left_half) and j < len(right_half):
28+
if left_half[i] < right_half[j]:
29+
arr[k] = left_half[i]
30+
i += 1
31+
else:
32+
arr[k] = right_half[j]
33+
j += 1
34+
k += 1
35+
36+
# Checking if any element remains in left_half
37+
while i < len(left_half):
38+
arr[k] = left_half[i]
39+
i += 1
40+
k += 1
41+
42+
# Checking if any element remains in right_half
43+
while j < len(right_half):
44+
arr[k] = right_half[j]
45+
j += 1
46+
k += 1
47+
48+
return arr
49+
50+
# Example usage:
51+
if __name__ == "__main__":
52+
example_arr = [38, 27, 43, 3, 9, 82, 10]
53+
print("Original Array:", example_arr)
54+
sorted_arr = merge_sort(example_arr)
55+
print("Sorted Array:", sorted_arr)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
def quick_sort(arr, start, end):
2+
"""
3+
Quick Sort algorithm: Sorts the array in ascending order.
4+
5+
Parameters:
6+
arr (list): The list of elements to be sorted.
7+
start (int): The starting index of the array segment.
8+
end (int): The ending index of the array segment.
9+
"""
10+
if start < end:
11+
# Partition the array and get the pivot index
12+
pivot_index = partition(arr, start, end)
13+
14+
# Recursively sort the left subarray
15+
quick_sort(arr, start, pivot_index - 1)
16+
17+
# Recursively sort the right subarray
18+
quick_sort(arr, pivot_index + 1, end)
19+
20+
21+
def partition(arr, start, end):
22+
"""
23+
Partitions the array around a pivot such that all elements smaller than
24+
or equal to the pivot are on the left, and all elements greater are on the right.
25+
26+
Parameters:
27+
arr (list): The list of elements to be partitioned.
28+
start (int): The starting index of the array segment.
29+
end (int): The ending index of the array segment (pivot).
30+
31+
Returns:
32+
int: The index of the pivot element after partitioning.
33+
"""
34+
pivot = arr[end] # Choose the last element as the pivot
35+
partition_index = start # Initialize the partition index
36+
37+
for i in range(start, end):
38+
if arr[i] <= pivot:
39+
# Swap if the current element is smaller than or equal to the pivot
40+
arr[i], arr[partition_index] = arr[partition_index], arr[i]
41+
partition_index += 1
42+
43+
# Place the pivot element at the correct position
44+
arr[partition_index], arr[end] = arr[end], arr[partition_index]
45+
return partition_index
46+
47+
48+
# Example Usage
49+
if __name__ == "__main__":
50+
# Input array
51+
array = [8, 3, 1, 7, 0, 10, 2]
52+
print("Original Array:", array)
53+
54+
# Perform Quick Sort
55+
quick_sort(array, 0, len(array) - 1)
56+
57+
# Output sorted array
58+
print("Sorted Array:", array)

0 commit comments

Comments
 (0)