Sort an Array of Numbers in Ascending Order in C++

In this exercise, you will implement a program that sorts an array of numbers in ascending order. Sorting is one of the most fundamental operations in programming, and there are several algorithms to achieve this, such as Bubble Sort, Selection Sort, and Quick Sort. For this exercise, you will focus on using a simple sorting algorithm to reorder the numbers from the smallest to the largest.

Sorting an array allows for better organization of data, easier searching, and more efficient algorithms. This problem is commonly encountered in various fields, from organizing lists of names to processing large datasets.

You'll create a C++ program that sorts an array of numbers in increasing order and then prints the sorted array.

Group

Arrays and Vectors in C++

Objective

1. Create a function that takes an array and its size as input.
2. Implement a sorting algorithm to rearrange the numbers in ascending order.
3. You can use a simple sorting algorithm such as Bubble Sort.
4. Print the sorted array to verify the result.
5. Ensure that the output displays the numbers from the smallest to the largest.

Create a program that sorts an array of numbers from smallest to largest.

Example C++ Exercise

 Copy C++ Code
#include <iostream> // Include iostream for input and output
using namespace std; // Use the standard namespace

// Function to sort an array in ascending order using Bubble Sort
void sortArray(int arr[], int size) {
    // Loop through each element in the array
    for (int i = 0; i < size - 1; i++) {
        // Loop through the array again, up to the last unsorted element
        for (int j = 0; j < size - i - 1; j++) {
            // If the current element is greater than the next, swap them
            if (arr[j] > arr[j + 1]) {
                // Swap the elements
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
}

// Function to print the elements of the array
void printArray(int arr[], int size) {
    // Loop through the array and print each element
    for (int i = 0; i < size; i++) {
        cout << arr[i] << " "; // Print each number followed by a space
    }
    cout << endl; // Print a newline after the array
}

int main() {
    // Define an array of numbers to be sorted
    int numbers[] = {34, 12, 5, 9, 27, 43};
    int size = sizeof(numbers) / sizeof(numbers[0]); // Calculate the size of the array

    // Call the sorting function
    sortArray(numbers, size);

    // Print the sorted array
    cout << "Sorted array: ";
    printArray(numbers, size);

    return 0; // End of program
}

 Output

Sorted array: 5 9 12 27 34 43

Share this C++ Exercise


More C++ Programming Exercises of Arrays and Vectors in C++

Explore our set of C++ Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C++. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C++.

  • Sum of Array Elements in C++

    In this exercise, you will create a program that calculates the sum of all the elements in an array. Summing the elements of an array is a common task in programming, which is ofte...

  • Search for a Number in an Array in C++

    In this exercise, you will create a program that searches for a specific number in an array. Searching through an array is a common task in programming, which is used in various ap...

  • Calculate the Average of Array Elements in C++

    In this exercise, you will create a function that calculates the average of the elements in an array. This is a common task when working with arrays, especially in data analysis, w...

  • Remove a Specific Element from an Array in C++

    In this exercise, you will develop a program that removes a specific element from an array. Removing elements from an array is a common task, especially when working with data that...

  • Find the Largest Number in an Array in C++

    In this exercise, you will implement a function that finds the largest number in an array. This task is useful when working with datasets where you need to extract the maximum valu...

  • Reverse an Array in C++

    In this exercise, you will implement a program that reverses an array. This task is useful in many scenarios such as reversing a list of elements, manipulating data structures, and...