Skip to content

Commit 23b92ba

Browse files
authored
Merge branch 'CodeHarborHub:main' into main
2 parents 3017a38 + 956198c commit 23b92ba

File tree

7 files changed

+1425
-0
lines changed

7 files changed

+1425
-0
lines changed
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
---
2+
id: Bubble-Sort
3+
title: Bubble Sort (Geeks for Geeks)
4+
sidebar_label: Bubble Sort
5+
tags:
6+
- Beginner
7+
- Sorting Algorithms
8+
- Geeks for Geeks
9+
- CPP
10+
- Python
11+
- Java
12+
- JavaScript
13+
- DSA
14+
description: "This is a solution to the Bubble Sort problem on Geeks for Geeks."
15+
---
16+
17+
## 1. What is Bubble Sort?
18+
19+
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 process is repeated until the list is sorted.
20+
21+
## 2. Algorithm for Bubble Sort
22+
23+
1. Start with the first element and compare it with the second element.
24+
2. If the first element is greater than the second element, swap them.
25+
3. Move to the next pair and repeat the process until the end of the list.
26+
4. Repeat steps 1-3 for all elements in the list until no swaps are needed.
27+
28+
## 3. How does Bubble Sort work?
29+
30+
- Bubble Sort works by repeatedly swapping adjacent elements if they are in the wrong order.
31+
- The largest element "bubbles up" to its correct position in each iteration.
32+
33+
## 4. Problem Description
34+
35+
Given an array of integers, implement the Bubble Sort algorithm to sort the array in ascending order.
36+
37+
## 5. Examples
38+
39+
**Example 1:**
40+
```
41+
Input: [64, 34, 25, 12, 22, 11, 90]
42+
Output: [11, 12, 22, 25, 34, 64, 90]
43+
```
44+
45+
**Example 2:**
46+
```
47+
Input: [5, 1, 4, 2, 8]
48+
Output: [1, 2, 4, 5, 8]
49+
```
50+
51+
**Explanation of Example 1:**
52+
- The initial array is [64, 34, 25, 12, 22, 11, 90].
53+
- First pass: [34, 25, 12, 22, 11, 64, 90].
54+
- Second pass: [25, 12, 22, 11, 34, 64, 90].
55+
- Continue until the array is sorted: [11, 12, 22, 25, 34, 64, 90].
56+
57+
## 6. Constraints
58+
59+
- $The array can have any number of elements.$
60+
- $All elements in the array are integers.$
61+
62+
## 7. Implementation
63+
64+
<Tabs>
65+
<TabItem value="Python" label="Python" default>
66+
<SolutionAuthor name="@ngmuraqrdd"/>
67+
```python
68+
def bubble_sort(arr):
69+
n = len(arr)
70+
for i in range(n):
71+
for j in range(0, n-i-1):
72+
if arr[j] > arr[j+1]:
73+
arr[j], arr[j+1] = arr[j+1], arr[j]
74+
return arr
75+
76+
# Example usage:
77+
arr = [64, 34, 25, 12, 22, 11, 90]
78+
bubble_sort(arr)
79+
print(arr)
80+
```
81+
</TabItem>
82+
83+
<TabItem value="C++" label="C++">
84+
<SolutionAuthor name="@ngmuraqrdd"/>
85+
```cpp
86+
#include <iostream>
87+
#include <vector>
88+
89+
void bubbleSort(std::vector<int>& arr) {
90+
int n = arr.size();
91+
for (int i = 0; i < n - 1; i++) {
92+
for (int j = 0; j < n - i - 1; j++) {
93+
if (arr[j] > arr[j + 1]) {
94+
std::swap(arr[j], arr[j + 1]);
95+
}
96+
}
97+
}
98+
}
99+
100+
int main() {
101+
std::vector<int> arr = {64, 34, 25, 12, 22, 11, 90};
102+
bubbleSort(arr);
103+
for (int num : arr)
104+
std::cout << num << " ";
105+
return 0;
106+
}
107+
```
108+
</TabItem>
109+
110+
<TabItem value="Java" label="Java">
111+
<SolutionAuthor name="@ngmuraqrdd"/>
112+
```java
113+
public class BubbleSort {
114+
public static void bubbleSort(int[] arr) {
115+
int n = arr.length;
116+
for (int i = 0; i < n - 1; i++) {
117+
for (int j = 0; j < n - i - 1; j++) {
118+
if (arr[j] > arr[j + 1]) {
119+
int temp = arr[j];
120+
arr[j] = arr[j + 1];
121+
arr[j + 1] = temp;
122+
}
123+
}
124+
}
125+
}
126+
127+
public static void main(String[] args) {
128+
int[] arr = {64, 34, 25, 12, 22, 11, 90};
129+
bubbleSort(arr);
130+
for (int num : arr)
131+
System.out.print(num + " ");
132+
}
133+
}
134+
```
135+
</TabItem>
136+
137+
<TabItem value="JavaScript" label="JavaScript">
138+
<SolutionAuthor name="@ngmuraqrdd"/>
139+
```javascript
140+
function bubbleSort(arr) {
141+
let n = arr.length;
142+
for (let i = 0; i < n - 1; i++) {
143+
for (let j = 0; j < n - i - 1; j++) {
144+
if (arr[j] > arr[j + 1]) {
145+
[arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
146+
}
147+
}
148+
}
149+
return arr;
150+
}
151+
152+
// Example usage:
153+
let arr = [64, 34, 25, 12, 22, 11, 90];
154+
bubbleSort(arr);
155+
console.log(arr);
156+
```
157+
</TabItem>
158+
</Tabs>
159+
160+
## 8. Complexity Analysis
161+
162+
- **Time Complexity**:
163+
- Best case: $O(n)$ (when the array is already sorted)
164+
- Average case: $O(n^2)$
165+
- Worst case: $O(n^2)$
166+
167+
- **Space Complexity**: $O(1)$ (in-place sorting)
168+
169+
## 9. Advantages and Disadvantages
170+
171+
**Advantages:**
172+
- Simple to understand and implement.
173+
- Works well with small datasets.
174+
175+
**Disadvantages:**
176+
- Inefficient for large datasets due to $O(n^2)$ time complexity.
177+
- Requires multiple passes through the list.
178+
179+
## 10. References
180+
181+
- **GFG Problem:** [GFG Problem](https://www.geeksforgeeks.org/bubble-sort/)
182+
- **HackerRank Problem:** [HackerRank](https://www.hackerrank.com/challenges/bubble-sort/problem)
183+
- **Author's Geeks for Geeks Profile:** [MuraliDharan](https://www.geeksforgeeks.org/user/ngmuraqrdd/)
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
---
2+
id: Insertion-Sort
3+
title: Insertion Sort (Geeks for Geeks)
4+
sidebar_label: Insertion Sort
5+
tags:
6+
- Beginner
7+
- Sorting Algorithms
8+
- Geeks for Geeks
9+
- CPP
10+
- Python
11+
- Java
12+
- JavaScript
13+
- DSA
14+
description: "This is a solution to the Insertion Sort problem on Geeks for Geeks."
15+
---
16+
17+
## 1. What is Insertion Sort?
18+
19+
Insertion Sort is a simple comparison-based sorting algorithm that 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.
20+
21+
## 2. Algorithm for Insertion Sort
22+
23+
1. Start with the first element (consider it as sorted).
24+
2. Pick the next element.
25+
3. Compare the picked element with the elements in the sorted part of the array.
26+
4. Shift all the elements in the sorted part that are greater than the picked element to one position to their right.
27+
5. Insert the picked element at its correct position.
28+
6. Repeat steps 2-5 until the entire array is sorted.
29+
30+
## 3. How does Insertion Sort work?
31+
32+
- Insertion Sort works by dividing the list into a sorted and an unsorted part.
33+
- It iteratively takes one element from the unsorted part and inserts it into the correct position in the sorted part.
34+
35+
## 4. Problem Description
36+
37+
Given an array of integers, implement the Insertion Sort algorithm to sort the array in ascending order.
38+
39+
## 5. Examples
40+
41+
**Example 1:**
42+
```
43+
Input: [12, 11, 13, 5, 6]
44+
Output: [5, 6, 11, 12, 13]
45+
```
46+
47+
**Example 2:**
48+
```
49+
Input: [31, 41, 59, 26, 41, 58]
50+
Output: [26, 31, 41, 41, 58, 59]
51+
```
52+
53+
**Explanation of Example 1:**
54+
- The initial array is [12, 11, 13, 5, 6].
55+
- First pass: [11, 12, 13, 5, 6].
56+
- Second pass: [11, 12, 13, 5, 6].
57+
- Third pass: [5, 11, 12, 13, 6].
58+
- Fourth pass: [5, 6, 11, 12, 13].
59+
60+
## 6. Constraints
61+
62+
- $The array can have any number of elements.$
63+
- $All elements in the array are integers.$
64+
65+
## 7. Implementation
66+
67+
<Tabs>
68+
<TabItem value="Python" label="Python" default>
69+
<SolutionAuthor name="@ngmuraqrdd"/>
70+
```python
71+
def insertion_sort(arr):
72+
for i in range(1, len(arr)):
73+
key = arr[i]
74+
j = i - 1
75+
while j >= 0 and key < arr[j]:
76+
arr[j + 1] = arr[j]
77+
j -= 1
78+
arr[j + 1] = key
79+
return arr
80+
81+
# Example usage:
82+
arr = [12, 11, 13, 5, 6]
83+
insertion_sort(arr)
84+
print(arr)
85+
```
86+
</TabItem>
87+
88+
<TabItem value="C++" label="C++">
89+
<SolutionAuthor name="@ngmuraqrdd"/>
90+
```cpp
91+
#include <iostream>
92+
#include <vector>
93+
94+
void insertionSort(std::vector<int>& arr) {
95+
int n = arr.size();
96+
for (int i = 1; i < n; i++) {
97+
int key = arr[i];
98+
int j = i - 1;
99+
while (j >= 0 && arr[j] > key) {
100+
arr[j + 1] = arr[j];
101+
j--;
102+
}
103+
arr[j + 1] = key;
104+
}
105+
}
106+
107+
int main() {
108+
std::vector<int> arr = {12, 11, 13, 5, 6};
109+
insertionSort(arr);
110+
for (int num : arr)
111+
std::cout << num << " ";
112+
return 0;
113+
}
114+
```
115+
</TabItem>
116+
117+
<TabItem value="Java" label="Java">
118+
<SolutionAuthor name="@ngmuraqrdd"/>
119+
```java
120+
public class InsertionSort {
121+
public static void insertionSort(int[] arr) {
122+
int n = arr.length;
123+
for (int i = 1; i < n; i++) {
124+
int key = arr[i];
125+
int j = i - 1;
126+
while (j >= 0 && arr[j] > key) {
127+
arr[j + 1] = arr[j];
128+
j = j - 1;
129+
}
130+
arr[j + 1] = key;
131+
}
132+
}
133+
134+
public static void main(String[] args) {
135+
int[] arr = {12, 11, 13, 5, 6};
136+
insertionSort(arr);
137+
for (int num : arr)
138+
System.out.print(num + " ");
139+
}
140+
}
141+
```
142+
</TabItem>
143+
144+
<TabItem value="JavaScript" label="JavaScript">
145+
<SolutionAuthor name="@ngmuraqrdd"/>
146+
```javascript
147+
function insertionSort(arr) {
148+
let n = arr.length;
149+
for (let i = 1; i < n; i++) {
150+
let key = arr[i];
151+
let j = i - 1;
152+
while (j >= 0 && arr[j] > key) {
153+
arr[j + 1] = arr[j];
154+
j = j - 1;
155+
}
156+
arr[j + 1] = key;
157+
}
158+
return arr;
159+
}
160+
161+
// Example usage:
162+
let arr = [12, 11, 13, 5, 6];
163+
insertionSort(arr);
164+
console.log(arr);
165+
```
166+
</TabItem>
167+
</Tabs>
168+
169+
## 8. Complexity Analysis
170+
171+
- **Time Complexity**:
172+
- Best case: $O(n)$ (when the array is already sorted)
173+
- Average case: $O(n^2)$
174+
- Worst case: $O(n^2)$
175+
176+
- **Space Complexity**: $O(1)$ (in-place sorting)
177+
178+
## 9. Advantages and Disadvantages
179+
180+
**Advantages:**
181+
- Simple to understand and implement.
182+
- Efficient for small datasets.
183+
- Adaptive: Efficient for data sets that are already substantially sorted.
184+
185+
**Disadvantages:**
186+
- Inefficient for large datasets due to $O(n^2)$ time complexity.
187+
188+
## 10. References
189+
190+
- **GFG Problem:** [GFG Problem](https://www.geeksforgeeks.org/insertion-sort/)
191+
- **HackerRank Problem:** [HackerRank](https://www.hackerrank.com/challenges/insertionsort1/problem)
192+
- **Author's Geeks for Geeks Profile:** [MuraliDharan](https://www.geeksforgeeks.org/user/ngmuraqrdd/)

0 commit comments

Comments
 (0)