C++ Sorting Algorithms (CSoA)



AVAILABLE:    5

Sorting is defined as reorganizing a set of objects according to certain rules. Types of sorting include:

  • Ascending order: Arranging data from the smallest to the largest.
  • Descending order: Arranging data from the largest to the smallest.

The advantages of sorted data include ease in searching for specific data, and convenience in updating, inserting new data, deleting, or merging datasets.


1. Direct Insertion Sorting

Pseudocode:

  1. Start with n = 1.
  2. Take the n-th element and find the insertion position based on the criteria.
  3. Shift elements from the insertion position up to the n-th element.
  4. Insert the element into the appropriate position.
  5. Increment n by 1.
  6. Repeat steps 2–5 until all elements are sorted.

Example:

Initial Data10512032563461199
Process 151012032563461199
Process 251012032563461199
Process 305121032563461199
Process 405101232563461199
Process 505101232345661199
Process 605610123234561199
Process 705610123234561199
Process 805610111232345699
Sorted05610111232345699

Program:

int j, x;
for (int i = 1; i < 10; i++) {
    x = arr[i];
    j = i - 1;
    while (x < arr[j]) {
        arr[j + 1] = arr[j];
        j--;
    }
    arr[j + 1] = x;
}

2. Direct Selection Sorting

This method selects the smallest (ascending) or largest (descending) element and places it at the leftmost position through swapping.

Pseudocode:

  1. Repeat steps 2–4 for i = 1 to n-1:
  2. Set location = i.
  3. For j = i+1 to n:
    • If arr[location] > arr[j], set location = j.
  4. Swap arr[location] with arr[i].

Example:

Initial Data10512032563461199
Process 101012532563461199
Process 205121032563461199
Process 305610325634121199
Process 405610325634121199
Process 505610115634123299
Process 605610111234563299
Process 705610111232563499
Process 805610111232345699

Program:

int location, temp;
for (int i = 0; i < 10; i++) {
    location = i;
    for (int j = i + 1; j < 10; j++) {
        if (arr[location] > arr[j])
            location = j;
    }
    temp = arr[location];
    arr[location] = arr[i];
    arr[i] = temp;
}

3. Bubble Sort

This method compares adjacent elements and swaps them if they are in the wrong order.

Pseudocode:

  1. For i = 1 to n:
    • For j = n to 1:
      • If arr[j-1] > arr[j], swap arr[j-1] and arr[j].

Program:

int temp;
for (int i = 1; i < 10; i++) {
    for (int j = 9; j > 0; j--) {
        if (arr[j - 1] > arr[j]) {
            temp = arr[j];
            arr[j] = arr[j - 1];
            arr[j - 1] = temp;
        }
    }
}

Example Program:

#include <iostream>
#include <conio.h>

void main() {
    int arr[10] = {10, 5, 12, 0, 32, 56, 34, 6, 11, 99};
    int x, j;

    std::cout << "Data before sorting: " << std::endl;
    for (int i = 0; i < 10; i++) 
        std::cout << arr[i] << " ";
    std::cout << std::endl;

    for (int i = 1; i < 10; i++) {
        x = arr[i];
        j = i - 1;
        while (x < arr[j] && j >= 0) {
            arr[j + 1] = arr[j];
            j--;
        }
        arr[j + 1] = x;
    }

    std::cout << "Data after sorting: " << std::endl;
    for (int i = 0; i < 10; i++) 
        std::cout << arr[i] << " ";

    getch();
}

Source: 

Data Structures course notes from STMIK EL Rahma Yogyakarta, authored by Eko Riswanto.


Post a Comment

Previous Next

نموذج الاتصال