Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
382 changes: 382 additions & 0 deletions Sorting/easy/Bubble sort/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,382 @@
# Introduction to Sorting and its techniques:

Sorting is a fundamental operation in computer science and data processing that involves arranging elements in a specific order. The goal of sorting is to make it easier to search for, retrieve, and analyze data. There are various sorting techniques, each with its own advantages and disadvantages, depending on factors such as the size of the dataset, the distribution of data, and the resources available.

Here is an introduction to sorting and some commonly used sorting techniques:

1. **Bubble Sort:**
- **Idea:** It repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. This process is repeated until the list is sorted.
- **Time Complexity:** O(n^2) in the worst and average cases, O(n) in the best case (when the list is already sorted).
2. **Selection Sort:**
- **Idea:** It divides the input list into two parts: a sorted and an unsorted region. In each iteration, it finds the minimum (or maximum) element from the unsorted region and swaps it with the first element of the unsorted region.
- **Time Complexity:** O(n^2) in all cases.
3. **Insertion Sort:**
- **Idea:** It builds the final sorted array one element at a time. It takes each element from the unsorted list and inserts it into its correct position in the sorted list.
- **Time Complexity:** O(n^2) in the worst case, O(n) in the best case.
4. **Merge Sort:**
- **Idea:** It employs a divide-and-conquer strategy. The unsorted list is divided into n sub-lists, each containing one element. These sub-lists are then repeatedly merged to produce new sorted sub-lists until there is only one sub-list remaining.
- **Time Complexity:** O(n log n) in all cases.
5. **Quick Sort:**
- **Idea:** It also uses a divide-and-conquer strategy. It selects a "pivot" element from the array and partitions the other elements into two sub-arrays according to whether they are less than or greater than the pivot. The process is then applied recursively to the sub-arrays.
- **Time Complexity:** O(n^2) in the worst case, O(n log n) on average.
6. **Heap Sort:**
- **Idea:** It uses a binary heap data structure to build a heap and then repeatedly extracts the maximum (for max heap) element from the heap, reconstructing the heap after each extraction.
- **Time Complexity:** O(n log n) in all cases.

Sorting algorithms are crucial in various applications, including databases, search algorithms, and computational biology. The choice of which algorithm to use depends on the specific requirements of the task at hand and the characteristics of the data to be sorted. Different algorithms have different time and space complexities, and the most suitable one often depends on the specific context in which it is being applied.

## Algorithm for Bubble Sort:

In bubble sort algorithm, traverse from left and compare adjacent elements and the higher one is placed at right side. In this way, the largest element is moved to the rightmost end at first. This process is then continued to find the second largest and place it and so on until the data is sorted.

Bubble Sort Algorithm is the simplest Sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity is quite high.

## Program for Bubble Sort in `cpp`:

```cpp
#include <iostream>
using namespace std;
```

```cpp
// Function to perform Bubble Sort
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n - 1; ++i) {
for (int j = 0; j < n - i - 1; ++j) {
// Compare adjacent elements
if (arr[j] > arr[j + 1]) {
// Swap them if they are in the wrong order
swap(arr[j], arr[j + 1]);
}
}
}
}
```

```cpp
// Function to print an array
void printArray(int arr[], int size) {
for (int i = 0; i < size; ++i) {
cout << arr[i] << " ";
}
cout << endl;
}
```

```cpp
int main() {
// Example array
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
```

```
cout << "Unsorted array: ";
printArray(arr, n);

// Call the bubbleSort function
bubbleSort(arr, n);

cout << "Sorted array: ";
printArray(arr, n);

return 0;

```

```cpp
}
```

## Step By Step Explanation in `cpp`:

**Explanation:**

1. **Include necessary headers:**

```cpp
cppCopy code
#include <iostream>using namespace std;
```

This includes the necessary input/output stream headers for the C++ program.

2. **`bubbleSort` Function:**

```cpp
cppCopy code
void bubbleSort(int arr[], int n) {
// Outer loop for the number of passes
for (int i = 0; i < n - 1; ++i) {
// Inner loop for each pass
// Last i elements are already sorted, so no need to check them
for (int j = 0; j < n - i - 1; ++j) {
// Compare adjacent elements
if (arr[j] > arr[j + 1]) {
// Swap them if they are in the wrong order
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

```

- The **`bubbleSort`** function implements the Bubble Sort algorithm.
- The outer loop (**`for (int i = 0; i < n - 1; ++i)`**) controls the number of passes through the array.
- The inner loop (**`for (int j = 0; j < n - i - 1; ++j)`**) iterates through the unsorted part of the array in each pass.
- The conditional statement checks if adjacent elements are in the wrong order, and if so, it swaps them.
3. **`printArray` Function:**

```cpp
cppCopy code
void printArray(int arr[], int size) {
for (int i = 0; i < size; ++i) {
cout << arr[i] << " ";
}
cout << endl;
}

```

- The **`printArray`** function is used to display the elements of an array.
4. **Main Function:**

```cpp
cppCopy code
int main() {
// Initialize an array
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);

// Display the unsorted array
cout << "Unsorted array: ";
printArray(arr, n);

// Call the bubbleSort function to sort the array
bubbleSort(arr, n);

// Display the sorted array
cout << "Sorted array: ";
printArray(arr, n);

return 0;
}

```

- The **`main`** function initializes an array and its size.
- It displays the unsorted array using the **`printArray`** function.
- Calls the **`bubbleSort`** function to sort the array.
- Displays the sorted array using the **`printArray`** function.

**Step-by-Step Execution:**

1. The unsorted array is displayed: **`64 34 25 12 22 11 90`**
2. The **`bubbleSort`** function is called:
- **Pass 1:** Comparing and swapping elements, the largest element (90) moves to the last position.
- **Pass 2:** Continue comparing and swapping, the second largest element moves to the second-last position. Continue this process till the end and the sorted array is displayed.
3. The sorted array displayed is: **`11 12 22 25 34 64 90`**

This is a basic example of the Bubble Sort algorithm in C++. Each pass of the algorithm moves the largest unsorted element to its correct position. The process repeats until the entire array is sorted.

## Program for Bubble sort in `python`:

```python
def bubble_sort(arr):
n = len(arr)

# Outer loop for the number of passes
for i in range(n - 1):
# Inner loop for each pass
# Last i elements are already sorted, so no need to check them
for j in range(n - i - 1):
# Compare adjacent elements
if arr[j] > arr[j + 1]:
# Swap them if they are in the wrong order
arr[j], arr[j + 1] = arr[j + 1], arr[j]

# Function to print an array
def print_array(arr):
for i in arr:
print(i, end=" ")
print()

# Example usage
arr = [64, 34, 25, 12, 22, 11, 90]

# Display the unsorted array
print("Unsorted array:", end=" ")
print_array(arr)

# Call the bubble_sort function to sort the array
bubble_sort(arr)

# Display the sorted array
print("Sorted array:", end=" ")
print_array(arr)

```

## Step By Step Explanation in `python`:

1. **`bubble_sort` Function:**

```python
pythonCopy code
def bubble_sort(arr):
n = len(arr)

# Outer loop for the number of passes
for i in range(n - 1):
# Inner loop for each pass
# Last i elements are already sorted, so no need to check them
for j in range(n - i - 1):
# Compare adjacent elements
if arr[j] > arr[j + 1]:
# Swap them if they are in the wrong order
arr[j], arr[j + 1] = arr[j + 1], arr[j]

```

- The **`bubble_sort`** function implements the Bubble Sort algorithm in Python.
- The outer loop (**`for i in range(n - 1)`**) controls the number of passes through the array.
- The inner loop (**`for j in range(n - i - 1)`**) iterates through the unsorted part of the array in each pass.
- The conditional statement checks if adjacent elements are in the wrong order, and if so, it swaps them.
2. **`print_array` Function:**

```python
pythonCopy code
def print_array(arr):
for i in arr:
print(i, end=" ")
print()

```

- The **`print_array`** function is used to display the elements of an array.
3. **Example Usage:**

```python
pythonCopy code
# Example usage
arr = [64, 34, 25, 12, 22, 11, 90]

# Display the unsorted array
print("Unsorted array:", end=" ")
print_array(arr)

# Call the bubble_sort function to sort the array
bubble_sort(arr)

# Display the sorted array
print("Sorted array:", end=" ")
print_array(arr)

```

- An example array is defined (**`arr = [64, 34, 25, 12, 22, 11, 90]`**).
- The unsorted array is displayed using the **`print_array`** function.
- The **`bubble_sort`** function is called to sort the array.
- The sorted array is displayed using the **`print_array`** function.

**Step-by-Step Execution:**

1. The unsorted array is displayed: **`64 34 25 12 22 11 90`**
2. The **`bubble_sort`** function is called:
- **Pass 1:** Compare and swap elements, the largest element (90) moves to the last position.
- **Pass 2:** Continue comparing and swapping, the second-largest element moves to the second-last position. Continue this process till the end and the sorted array is displayed.
3. The sorted array is displayed: **`11 12 22 25 34 64 90`**

This is a basic example of the Bubble Sort algorithm in Python. The algorithm compares and swaps adjacent elements in each pass, gradually moving the larger elements toward the end of the array. The process repeats until the entire array is sorted.

## Program for Bubble sort in `java`:

```cpp
public class BubbleSort {
// Function to perform Bubble Sort
static void bubbleSort(int arr[]) {
int n = arr.length;
```

```
// Outer loop for the number of passes
for (int i = 0; i < n - 1; ++i) {
// Inner loop for each pass
// Last i elements are already sorted, so no need to check them
for (int j = 0; j < n - i - 1; ++j) {
// Compare adjacent elements
if (arr[j] > arr[j + 1]) {
// Swap them if they are in the wrong order
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

// Function to print an array
static void printArray(int arr[]) {
int n = arr.length;
for (int i = 0; i < n; ++i) {
System.out.print(arr[i] + " ");
}
System.out.println();
}

public static void main(String args[]) {
// Example array
int arr[] = {64, 34, 25, 12, 22, 11, 90};

// Print the unsorted array
System.out.print("Unsorted array: ");
printArray(arr);

// Call the bubbleSort function
bubbleSort(arr);

// Print the sorted array
System.out.print("Sorted array: ");
printArray(arr);
}

```

```cpp
}
```

## Step by Step Explanation in `java`:

1. The program defines a class **`BubbleSort`** with two static methods: **`bubbleSort`** and **`printArray`**.
2. The **`bubbleSort`** method implements the Bubble Sort algorithm. It uses two nested loops for passes and comparisons.
3. An array **`arr`** is declared with some example values, and the **`bubbleSort`** method is called to sort it.
4. The **`printArray`** method is used to display the elements of an array.
5. The unsorted and sorted arrays are printed in the **`main`** method.

**Execution:**

- The unsorted array is: 64 34 25 12 22 11 90
- After sorting, the array becomes: 11 12 22 25 34 64 90

Bubble Sort repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. The process is repeated for each pass until the array is sorted. The time and space complexities are the same as mentioned in the previous response for Bubble Sort in general.

## Real time applications of Bubble sort:

Bubble Sort is a simple sorting algorithm that is not very efficient for large datasets. However, due to its simplicity, it may find application in certain scenarios where efficiency is not a critical concern. Here are a few potential real-time applications of Bubble Sort:

1. **Educational Purposes:**
- Bubble Sort is often used in introductory computer science courses to teach the concept of sorting algorithms. Its straightforward implementation makes it a good starting point for students to understand sorting techniques.
2. **Small Datasets:**
- When dealing with small datasets or nearly sorted data, Bubble Sort might be a reasonable choice. Its simplicity makes it easy to implement, and for small datasets, the performance difference compared to more efficient algorithms might not be significant.
3. **Embedded Systems:**
- In resource-constrained environments such as embedded systems with limited memory and processing power, Bubble Sort could be used for simplicity and code size, especially when the dataset size is known to be small.
4. **Hardware Design:**
- In certain hardware design scenarios, where simplicity is prioritized over speed, Bubble Sort might be used. This is more common in educational or prototyping contexts rather than production-level hardware.
5. **Human-Interactive Sorting:**
- In scenarios where a user is providing input in real-time, and the dataset is small, the simplicity of Bubble Sort might be preferred for its ease of understanding. For example, sorting a small list of items in a user interface.

It's important to note that in most real-world applications where efficiency is crucial, more advanced sorting algorithms like Quick Sort, Merge Sort, or even built-in sorting functions provided by programming languages (e.g., Java's **`Arrays.sort()`** or Python's **`sorted()`**) are preferred. Bubble Sort's time complexity of O(n^2) makes it less suitable for large datasets compared to more efficient algorithms.