Knowee
Questions
Features
Study Tools

C program to sort an array in ascending order

Question

C program to sort an array in ascending order

🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a simple C program that sorts an array in ascending order using the bubble sort algorithm:

#include <stdio.h>

void bubbleSort(int array[], int size) {
  for (int step = 0; step < size - 1; ++step) {
    for (int i = 0; i < size - step - 1; ++i) {
      if (array[i] > array[i + 1]) {
        int temp = array[i];
        array[i] = array[i + 1];
        array[i + 1] = temp;
      }
    }
  }
}

void printArray(int array[], int size) {
  for (int i = 0; i < size; ++i) {
    printf("%d  ", array[i]);
  }
  printf("\n");
}

int main() {
  int data[] = {-2, 45, 0, 11, -9};
  int size = sizeof(data) / sizeof(data[0]);
  bubbleSort(data, size);
  printf("Sorted Array in Ascending Order:\n");
  printArray(data, size);
}

This program first defines a function bubbleSort that sorts the array. The outer loop step runs n-1 times, where n is the size of the array. The inner loop compares each element with the next one and swaps them if the current element is greater.

The printArray function prints the elements of the array.

In the main function, we define an array data and calculate its size. Then we call bubbleSort to sort the array, and printArray to print the sorted

This problem has been solved

Similar Questions

Write a program to implement the Selection Sort algorithm to sort an array of integers in ascending order.Input:91 3 5 7 9 2 4 6 8Output:1 2 3 4 5 6 7 8 9Explanation:Initially, the minimum element in the array is 1. It is swapped with the first element, resulting in the array [1, 3, 8, 2, 5, 4]. Next, the minimum element in the remaining unsorted part is 2. It is swapped with the second element, resulting in [1, 2, 8, 3, 5, 4]. This process continues, and the final sorted array is [1, 2, 3, 4, 5, 8].

Sorting ProgramIn answering each questions, consider the program below:#include <iostream>#include <algorithm>using namespace std;// Function to perform Bubble Sort on an arrayvoid bubbleSort(int arr[], int n) { for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - 1 - i; j++) { // Swap if the element found is greater than the next element if (arr[j] > arr[j + 1]) { swap(arr[j], arr[j + 1]); } } }}int main() { // Given array of 10 numbers int numbers[] = {4, 2, 7, 1, 9, 5, 8, 3, 10, 6}; int n = sizeof(numbers) / sizeof(numbers[0]); // Call the bubbleSort function to sort the array bubbleSort(numbers, n); // Display the sorted array cout << "Sorted array: "; for (int i = 0; i < n; i++) { cout << numbers[i] << " "; } return 0;}---------------------------------------------------------------------------Definition of Terms in the given questions below:In the context of the Bubble Sort algorithm, a "pass" refers to a complete iteration through the array. During each pass, the algorithm compares adjacent elements and swaps them if they are in the wrong order. After each pass, the largest unsorted element is guaranteed to be in its correct position.The number of passes needed to sort an array of size n is generally n - 1. This is because, after the first pass, the largest element is in its correct place, and after the second pass, the second largest element is in its correct place, and so on. After n - 1 passes, the smallest element is also in its correct place, and the array is fully sorted.For example, if you have an array of 5 elements, you would typically need 4 passes to sort it. Each pass contributes to placing one more element in its correct position within the sorted portion of the array.

You are given an integer array arr. Sort the integers in the array in ascending order by the number of 1's in their binary representation and in case of two or more integers have the same number of 1's you have to sort them in ascending order.

You are tasked with implementing one of the classic sorting algorithms: Bubble Sort, Insertion Sort, or Selection Sort. Your goal is to sort an array of integers in non-decreasing order using the chosen algorithm.Input FormatThe input consists of two lines:The first line contains an integer n (1 ≤ n ≤ 100), representing the number of elements in the array.The second line contains n space-separated integers, representing the elements of the unsorted array. These integers are in the range -1000 to 1000.ConstraintsImplement the chosen sorting algorithm to sort the array in-place. Do not use any built-in sorting functions.The array elements are integers in the range -1000 to 1000.Output FormatYou should output a single line with n space-separated integers, representing the elements of the array in non-decreasing order after applying the selected sorting algorithm.Sample Input 0102 3 4 5 1 6 7 8 9 0Sample Output 00 1 2 3 4 5 6 7 8 9Explanation 0It appears you have two lines of input. The first line contains the number 10, which likely represents the total number of elements in the list. The second line contains ten space-separated integers: 2 3 4 5 1 6 7 8 9 0. These are the elements you want to sort in non-decreasing order using a sorting algorithm.So, the sorted version of the list you provided is 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, in non-decreasing order.Sample Input 162 4 5 4 6 2Sample Output 12 2 4 4 5 6

c program to insert the values at the specific index in the array.

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.