0% found this document useful (0 votes)
18 views

Sorting

Uploaded by

pranshusahu862
Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Sorting

Uploaded by

pranshusahu862
Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 4

Sorting Algorithms in Computer Science

Sorting is a fundamental operation in computer science, used to arrange data in a particular order.
The order can be ascending or descending and is essential for efficient searching, data management,
and other algorithmic applications.

1. Introduction to Sorting
Sorting organizes a collection of elements, such as numbers or strings, in a defined sequence.
Sorting is crucial for:
• Optimizing search algorithms (e.g., binary search).
• Data visualization and understanding.
• Reducing computational complexity in larger systems.

2. Types of Sorting Algorithms


Sorting algorithms are broadly categorized into:
1. Comparison-Based Sorting: Compares elements to determine their order.
• Examples: Bubble Sort, Merge Sort, Quick Sort.
2. Non-Comparison-Based Sorting: Uses other properties of data, such as counting or
hashing.
• Examples: Counting Sort, Radix Sort.

3. Basic Sorting Algorithms


3.1 Bubble Sort
• Description: Repeatedly swaps adjacent elements if they are in the wrong order.
• Time Complexity:
• Best Case: O(n) (Already sorted).
• Worst Case: O(n2) (Reversed order).
• Code (Python):
python
Copy code
def bubble_sort(arr):
n = len(arr)
for i in range(n):
for j in range(0, n - i - 1):
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]

3.2 Selection Sort


• Description: Selects the smallest element and swaps it with the first unsorted element.
• Time Complexity: O(n2).
• Code (Python):
python
Copy code
def selection_sort(arr):
n = len(arr)
for i in range(n):
min_idx = i
for j in range(i + 1, n):
if arr[j] < arr[min_idx]:
min_idx = j
arr[i], arr[min_idx] = arr[min_idx], arr[i]

3.3 Insertion Sort


• Description: Builds a sorted array one element at a time by inserting elements into their
correct positions.
• Time Complexity: O(n2).
• Code (Python):
python
Copy code
def insertion_sort(arr):
for i in range(1, len(arr)):
key = arr[i]
j = i - 1
while j >= 0 and key < arr[j]:
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = key

4. Efficient Sorting Algorithms


4.1 Merge Sort
• Description: Uses divide-and-conquer to recursively split the array and merge it in sorted
order.
• Time Complexity: O(nlogn).
• Code (Python):
python
Copy code
def merge_sort(arr):
if len(arr) > 1:
mid = len(arr) // 2
left = arr[:mid]
right = arr[mid:]

merge_sort(left)
merge_sort(right)

i = j = k = 0

while i < len(left) and j < len(right):


if left[i] < right[j]:
arr[k] = left[i]
i += 1
else:
arr[k] = right[j]
j += 1
k += 1

while i < len(left):


arr[k] = left[i]
i += 1
k += 1

while j < len(right):


arr[k] = right[j]
j += 1
k += 1

4.2 Quick Sort


• Description: Partitions the array into smaller sub-arrays using a pivot and sorts them.
• Time Complexity:
• Best and Average Case: O(nlogn).
• Worst Case: O(n2) (Unbalanced partitioning).
• Code (Python):
python
Copy code
def quick_sort(arr):
if len(arr) <= 1:
return arr
pivot = arr[len(arr) // 2]
left = [x for x in arr if x < pivot]
middle = [x for x in arr if x == pivot]
right = [x for x in arr if x > pivot]
return quick_sort(left) + middle + quick_sort(right)

5. Non-Comparison Sorting Algorithms


5.1 Counting Sort
• Description: Counts occurrences of each element and sorts based on counts.
• Time Complexity: O(n+k), where k is the range of input.
• Code (Python):
python
Copy code
def counting_sort(arr):
max_val = max(arr)
count = [0] * (max_val + 1)
for num in arr:
count[num] += 1
sorted_arr = []
for i, freq in enumerate(count):
sorted_arr.extend([i] * freq)
return sorted_arr

5.2 Radix Sort


• Description: Processes digits of numbers from least to most significant.
• Time Complexity: O(nk), where k is the number of digits.
• Code (Python):
python
Copy code
def counting_sort_exp(arr, exp):
n = len(arr)
output = [0] * n
count = [0] * 10

for num in arr:


count[(num // exp) % 10] += 1

for i in range(1, 10):


count[i] += count[i - 1]

for i in range(n - 1, -1, -1):


output[count[(arr[i] // exp) % 10] - 1] = arr[i]
count[(arr[i] // exp) % 10] -= 1

for i in range(n):
arr[i] = output[i]

def radix_sort(arr):
max_val = max(arr)
exp = 1
while max_val // exp > 0:
counting_sort_exp(arr, exp)
exp *= 10

6. Comparison of Sorting Algorithms


Algorithm Time Complexity Space Complexity Stable
Bubble Sort O(n2) O(1) Yes
Selection Sort O(n2) O(1) No
Insertion Sort O(n2) O(1) Yes
Merge Sort O(nlogn) O(n) Yes
Quick Sort O(nlogn)* O(logn) No
Counting Sort O(n+k) O(n+k) Yes
Radix Sort O(nk) O(n+k) Yes
*Worst-case time complexity is O(n2) for Quick Sort.

7. Applications of Sorting
• Data Analysis: Sorting facilitates efficient data aggregation and summarization.
• Database Systems: Used in indexing and query optimization.
• Computer Graphics: Sorting is used in rendering algorithms.
• Networks: Sorting helps in managing packet priorities.

You might also like