5-5-Array Sorting - Practice Problems - GeeksforGeeks
5-5-Array Sorting - Practice Problems - GeeksforGeeks
Sorting an array means arranging the elements of the array in a certain order. Generally sorting in an array is done
to arrange the elements in increasing or decreasing order.
Problem statement: Given an array of integers arr, the task is to sort the array in ascending order and return
it, without using any built-in functions.
Example:
To learn more about all other types of Sorting Algorithms, refer to the below articles:
1
#include <bits/stdc++.h>
2
using namespace std;
4
// Function to sort the array using Bubble Sort
5
void sortArray(vector<int>& arr){
6
int n = arr.size();
8
for (int i = 0; i < n - 1; i++) {
9
bool swapped = false;
10
11
// Last i elements are already in place
12
for (int j = 0; j < n - i - 1; j++) {
13
if (arr[j] > arr[j + 1]) {
14
swap(arr[j], arr[j + 1]);
15
Output
Sorted array:
11 12 22 25 34 64 90
In-Place Sorting: In-place sorting means arranging the elements without using any extra space other than the
given array.
Stability of Sorting: A sorting algorithm is said to be stable if the relative order of the same valued elements is
preserved in the final sorted array i.e., the relative order of the same valued elements in the sorted array is the
same as in the original array.