Sorting an array of integers in ascending order is a common problem in computer science. It involves arranging the numbers in the array so that they are ordered from the smallest to the largest. Let’s break down the process, explain it in detail, provide an example, and then show how to implement it in various programming languages.
Understanding the Problem
Given an array of integers, the goal is to rearrange the elements so that each element is less than or equal to the element that follows it. For example, consider the array:
[ [4, 2, 7, 1, 9] ]
The sorted array in ascending order will be:
[ [1, 2, 4, 7, 9] ]
Step-by-Step Approach
- Choose a Sorting Algorithm: There are several algorithms to sort an array, such as Bubble Sort, Selection Sort, Insertion Sort, Merge Sort, and Quick Sort. For simplicity, we will use Bubble Sort in the explanation, which is easy to understand.
- Implement the Algorithm: Bubble Sort works by repeatedly stepping through the list, comparing adjacent elements, and swapping them if they are in the wrong order. The process is repeated until the list is sorted.
Bubble Sort Algorithm Explained
- Initialization: Start with the first element of the array.
- Comparison: Compare the current element with the next one.
- Swap if Needed: If the current element is greater than the next element, swap them.
- Repeat: Continue this process for each pair of adjacent elements. After each pass through the array, the largest unsorted element will “bubble up” to its correct position.
- Finish: Repeat the process until no more swaps are needed.
Example Walkthrough
Consider the array: [ [4, 2, 7, 1, 9] ]
- Pass 1:
- Compare
4
and2
. Swap them: [ [2, 4, 7, 1, 9] ] - Compare
4
and7
. No swap needed. - Compare
7
and1
. Swap them: [ [2, 4, 1, 7, 9] ] - Compare
7
and9
. No swap needed. - Pass 2:
- Compare
2
and4
. No swap needed. - Compare
4
and1
. Swap them: [ [2, 1, 4, 7, 9] ] - Compare
4
and7
. No swap needed. - Pass 3:
- Compare
2
and1
. Swap them: [ [1, 2, 4, 7, 9] ] - Compare
2
and4
. No swap needed. - Pass 4:
- Compare
1
and2
. No swap needed.
The array is now sorted: [ [1, 2, 4, 7, 9] ]
Code Examples
1. Python
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]
return arr
# Example usage
arr = [4, 2, 7, 1, 9]
print("Sorted array:", bubble_sort(arr))
2. JavaScript
function bubbleSort(arr) {
let n = arr.length;
for (let i = 0; i < n; i++) {
for (let j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
[arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
}
}
}
return arr;
}
// Example usage
const arr = [4, 2, 7, 1, 9];
console.log("Sorted array:", bubbleSort(arr));
3. Java
public class Main {
public static void bubbleSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
public static void main(String[] args) {
int[] arr = {4, 2, 7, 1, 9};
bubbleSort(arr);
System.out.print("Sorted array: ");
for (int num : arr) {
System.out.print(num + " ");
}
}
}
4. C++
#include <iostream>
#include <vector>
void bubbleSort(std::vector<int>& arr) {
int n = arr.size();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
std::swap(arr[j], arr[j + 1]);
}
}
}
}
int main() {
std::vector<int> arr = {4, 2, 7, 1, 9};
bubbleSort(arr);
std::cout << "Sorted array: ";
for (int num : arr) {
std::cout << num << " ";
}
std::cout << std::endl;
return 0;
}
5. C
using System;
class Program {
public static void BubbleSort(int[] arr) {
int n = arr.Length;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
static void Main() {
int[] arr = {4, 2, 7, 1, 9};
BubbleSort(arr);
Console.Write("Sorted array: ");
foreach (int num in arr) {
Console.Write(num + " ");
}
Console.WriteLine();
}
}
Summary
Sorting an array involves rearranging the elements in a specific order. Bubble Sort is a simple sorting algorithm that works well for small arrays and is useful for understanding the basic principles of sorting. The provided code examples demonstrate how to implement Bubble Sort in Python, JavaScript, Java, C++, and C#. For larger or more complex sorting tasks, more efficient algorithms like Merge Sort or Quick Sort might be used, but the fundamental concept remains the same.