Sorting an array in Object-Oriented Programming(OOP) means arranging its elements in a specific order, such as ascending or descending. Here’s are some C++ program to implement sorting of arrays. There are various sorting algorithms available, each with its own characteristics and time complexities. Also, the common methods of C++ program to implement sorting of Arrays are written as,
Table of Contents
ToggleCommon Methods
Bubble Sorting (Ascending Order)
Selection sorting of arrays
Insertion sorting of Arrays
Quick sorting of Arrays
Bubble Sorting
The bubble sort algorithm goes through the list multiple times, examining adjacent items and performing swaps when needed. This sequence of steps repeats until the list is entirely sorted
These examples demonstrate sorting an array in ascending order using bubble sort and quick sort algorithms. You can adapt the code to sort in descending order or use other sorting algorithms as needed.
Example (Bubble Sort)
#include <iostream>
using namespace std;
int main()
{
int n;
cout << "enter numbers" << endl;
cin >> n;
int arr[5];
cout << "enter numbers that you want to sort=" << endl;
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
int counter = 1;
while (counter < n) {
for (int i = 0; i < n - counter; i++) {
if (arr[i] > arr[i + 1]) {
//swap arr[i] and arr[i+1]
int temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
}
counter++;
}
for (int i = 0; i < n; i++) {
cout << arr[i] << endl;
}
}
In this code we use for loop that controls the number of passes and then compares the adjacent elements and swaps them if they are out of order.
We swap elements by using a temporary variable temp.
The sorting process continues until the entire array is sorted.
In the main function, we create an integer array ‘arr’ with unsorted elements.
We print the original array. and then for loop swap the array in ascending order.
Finally , we print the sorted array.
Selection sorting Of Arrays
Selection sorting of arrays is a simple sorting algorithm that repeatedly selects the minimum (or maximum) element from the unsorted part of the array and places it at the beginning of the sorted part.
Selection sort starts by finding the smallest element in the unsorted part of the array and placing it at the beginning.
The array is then split into two sections:
the left part is the sorted subarray, and the right part is the unsorted subarray. After sorting the first element, the process continues with the search for the second smallest element in the remaining unsorted portion
Another Example
#include<iostream>
using namespace std;
int main()
{
int n;
cin >> n;
int arr[5];
for (int i = 0; 1 < n; i++) {
cin >> arr[i];
}
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[i]) {
int temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
}
}
for (int i = 0; i < n; i++) {
cout << arr[i] << endl;
}
}
Explanation
We declare a variable “n” that takes an array and its size as parameters.
The for loop control the position where the minimum element will be placed, starting from the first position and moving to the end of the array.
In the inner loop, we find the index of the minimum element in the unsorted part of the array.
We swap the minimum element with the element at the current position in the outer loop.
Until the list is fully ordered, this procedure continues.
In the main function, we create an integer array ‘arr’ with unsorted elements.
We print the original array. and then for loop swap the array in ascending order.
Finally , we print the sorted array.
Insertion sorting of arrays
Insertion sorting of arrays is a simple sorting algorithm that builds the final sorted array one element at a time.
this sorting method involves arranging elements by comparing each element with its preceding element. It begins by analyzing the relationship between the second element and the first element. the second element should be smaller then the first then we will swap it.
after that, we proceed to evaluate the third element by comparing it to all preceding elements. This pattern repeats for the fourth element and so on. When all comparisons have been completed, the elements attain their sorted order.
Example
#include<iostream>
using namespace std;
int main()
{
int n;
cin >> n;
int arr[5];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
for (int i = 1; i < n; i++) {
int counter = arr[i];
int j = i - 1;
while (arr[j]>counter){
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = counter;
}
for (int i = 0; i < n; i++) {
cout << arr[i] << endl;
}
Quick Sorting
Quick sorting of arrays in C++ is a widely used sorting algorithm for arrays. It is known for its efficiency and is considered one of the fastest sorting algorithms for most practical applications. It employs the divide-and-conquer method, which involves breaking the array into subarrays. Once these subarrays are sorted, they are merged to form the comprehensive sorted array.
in this approach, a central element is selected, and the array is partitioned into two halves based on the central element. The elements that are smaller than the central element is shifted to the left side of it, and the elements greater than the central element are moved to the right side.
- Divide: The algorithm selects a “center” element from the array. The choice of the center can vary, but it’s often the middle element. The array is then partitioned into two subarrays: one containing element less than the center and another containing element greater than the center.
- Conquer: Quicksort is recursively applied to both subarrays, sorting them independently. This process continues until the subarrays have only one or zero elements, as one-element subarrays are already considered sorted.
- Combine: As the recursion unfolds, the sorted subarrays are merged to form the final sorted array. The central element, which was used for partitioning, is in its correct sorted position.