Member-only story
In C++, how to sort an array into numerical or alphabetical order
1 min readMay 4, 2022
In order to sort an array, an outer for loop to iterate through each element array is declared. Within the outer for loop, there will be an inner for loop that iterates from i+1 to n, where there will be a comparison if the current element a[j] is less than the a[min]. If the element 1 is greater than element 2, then there will be a swap of the elements. It is important to store a[i] into a temp variable for the swap, since a[i] will have a different value.
void sortArray(string a[], int n) {int i, j, min;string temp;for (i = 0; i < n-1; i++) {min = i;for (j = i+1; j < n; j++){if (a[j] < a[min]){min = j;}temp = a[i];a[i] = a[min];a[min] = temp;}}}