Skip to content

Commit 5c6a296

Browse files
authored
Update README.md
1 parent fbb95d1 commit 5c6a296

1 file changed

Lines changed: 156 additions & 0 deletions

File tree

sorting-algorithms/README.md

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,157 @@
1+
# Sorting Algorithms in Python
12

3+
This repository contains implementations of various sorting algorithms in Python. Sorting is a fundamental operation in computer science and is crucial for optimizing the performance of other algorithms that require sorted data.
4+
5+
## Table of Contents
6+
7+
- [Overview](#overview)
8+
- [Algorithms Implemented](#algorithms-implemented)
9+
- [Requirements](#requirements)
10+
- [Usage](#usage)
11+
- [Algorithm Details](#algorithm-details)
12+
- [Bubble Sort](#bubble-sort)
13+
- [Selection Sort](#selection-sort)
14+
- [Insertion Sort](#insertion-sort)
15+
- [Merge Sort](#merge-sort)
16+
- [Quick Sort](#quick-sort)
17+
- [Heap Sort](#heap-sort)
18+
- [Performance Comparison](#performance-comparison)
19+
- [Contributing](#contributing)
20+
- [License](#license)
21+
22+
## Overview
23+
24+
This repository serves as a learning tool for understanding and comparing different sorting algorithms. Each algorithm is implemented in a standalone Python file with comments explaining the key steps.
25+
26+
## Algorithms Implemented
27+
28+
1. Bubble Sort
29+
2. Selection Sort
30+
3. Insertion Sort
31+
4. Merge Sort
32+
5. Quick Sort
33+
6. Heap Sort
34+
35+
## Requirements
36+
37+
- Python 3.x
38+
39+
## Usage
40+
41+
Clone the repository and navigate to the directory:
42+
43+
```sh
44+
git clone https://github.com/your-username/sorting-algorithms.git
45+
cd sorting-algorithms
46+
```
47+
48+
You can run any of the sorting algorithms by executing the corresponding Python file. For example, to run the Bubble Sort algorithm:
49+
50+
```sh
51+
python bubble_sort.py
52+
```
53+
54+
Each script includes a sample list and outputs the sorted result.
55+
56+
## Algorithm Details
57+
58+
### Bubble Sort
59+
60+
Bubble Sort is a simple comparison-based sorting algorithm. It repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. The pass through the list is repeated until the list is sorted.
61+
62+
**Time Complexity**: O(n^2)
63+
64+
```python
65+
def bubble_sort(arr):
66+
n = len(arr)
67+
for i in range(n):
68+
for j in range(0, n-i-1):
69+
if arr[j] > arr[j+1]:
70+
arr[j], arr[j+1] = arr[j+1], arr[j]
71+
return arr
72+
```
73+
74+
### Selection Sort
75+
76+
Selection Sort divides the input list into two parts: the sorted part and the unsorted part. It repeatedly selects the smallest (or largest) element from the unsorted part and moves it to the sorted part.
77+
78+
**Time Complexity**: O(n^2)
79+
80+
```python
81+
def selection_sort(arr):
82+
n = len(arr)
83+
for i in range(n):
84+
min_idx = i
85+
for j in range(i+1, n):
86+
if arr[j] < arr[min_idx]:
87+
min_idx = j
88+
arr[i], arr[min_idx] = arr[min_idx], arr[i]
89+
return arr
90+
```
91+
92+
### Insertion Sort
93+
94+
Insertion Sort builds the final sorted array one item at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort.
95+
96+
**Time Complexity**: O(n^2)
97+
98+
```python
99+
def insertion_sort(arr):
100+
for i in range(1, len(arr)):
101+
key = arr[i]
102+
j = i-1
103+
while j >= 0 and key < arr[j]:
104+
arr[j+1] = arr[j]
105+
j -= 1
106+
arr[j+1] = key
107+
return arr
108+
```
109+
110+
### Merge Sort
111+
112+
Merge Sort is a divide-and-conquer algorithm. It divides the input array into two halves, calls itself for the two halves, and then merges the two sorted halves.
113+
114+
**Time Complexity**: O(n log n)
115+
116+
```python
117+
def merge_sort(arr):
118+
if len(arr) > 1:
119+
mid = len(arr) // 2
120+
L = arr[:mid]
121+
R = arr[mid:]
122+
123+
merge_sort(L)
124+
merge_sort(R)
125+
126+
i = j = k = 0
127+
128+
while i < len(L) and j < len(R):
129+
if L[i] < R[j]:
130+
arr[k] = L[i]
131+
i += 1
132+
else:
133+
arr[k] = R[j]
134+
j += 1
135+
k += 1
136+
137+
while i < len(L):
138+
arr[k] = L[i]
139+
i += 1
140+
k += 1
141+
142+
while j < len(R):
143+
arr[k] = R[j]
144+
j += 1
145+
k += 1
146+
return arr
147+
```
148+
149+
### Quick Sort
150+
151+
Quick Sort is another divide-and-conquer algorithm. It picks an element as a pivot and partitions the given array around the picked pivot. There are different versions of quicksort that pick pivot in different ways.
152+
153+
**Time Complexity**: O(n log n)
154+
155+
```python
156+
def quick_sort(arr):
157+
if len

0 commit comments

Comments
 (0)