Knowee
Questions
Features
Study Tools

complete the precode to perform sorting operations using a pointer to functions input: assign an array of values as {5, 2, 8, 1, 6} exactly,  assign M value based on input given above.and declare a pointer variable to  assign this array,call the function swap to perform the bubble sort and output will be12568

Question

complete the precode to perform sorting operations using a pointer to functions input: assign an array of values as {5, 2, 8, 1, 6} exactly,  assign M value based on input given above.and declare a pointer variable to  assign this array,call the function swap to perform the bubble sort and output will be12568

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

Solution

Here is a step-by-step guide to complete the precode to perform sorting operations using a pointer to functions. We will use the C programming language for this task.

  1. Declare an array with the given values.
int arr[] = {5, 2, 8, 1, 6};
  1. Declare a pointer variable and assign the array to it.
int *p = arr;
  1. Declare the swap function. This function will take two pointers as arguments and swap the values they point to.
void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}
  1. Implement the bubble sort algorithm. This algorithm repeatedly steps through the list, compares adjacent elements and swaps them if they are in the wrong order. The pass through the list is repeated until the list is sorted.
void bubbleSort(int *arr, int n) {
    for (int i = 0; i < n-1; i++) {
        for (int j = 0; j < n-i-1; j++) {
            if (arr[j] > arr[j+1]) {
                swap(&arr[j], &arr[j+1]);
            }
        }
    }
}
  1. Call the bubbleSort function with the array and its size as arguments.
bubbleSort(p, 5);
  1. Print the sorted array.
for (int i = 0; i < 5; i++) {
    printf("%d", p[i]);
}

The complete code will look like this:

#include <stdio.h>

void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

void bubbleSort(int *arr, int n) {
    for (int i = 0; i < n-1; i++) {
        for (int j = 0; j < n-i-1; j++) {
            if (arr[j] > arr[j+1]) {
                swap(&arr[j], &arr[j+1]);
            }
        }
    }
}

int main() {
    int arr[] = {5, 2, 8, 1, 6};
    int *p = arr;
    bubbleSort(p, 5);
    for (int i = 0; i < 5; i++) {
        printf("%d", p[i]);
    }
    return 0;
}

This program will output 12568, which is the sorted array.

This problem has been solved

Similar Questions

Bubble Sort is a popular sorting algorithm. It works by repeatedly swapping adjacent elements thatare out of order.a) Write the pseudo code of the bubblesort algorithmb) Implement the bubblesort algorithm.c) Read 8 numbers from the keyboard and store them in an array. Sort the numbers usingthe bubble sort algorithm.

Give the result of bubble sort after fifth pass {94,78,65,45,58,72,63,12,18}and also write a function to implement bubble sort

Write a description of bubble sort in programming, give the steps to follow, keep it short

Attach your solution Here  for the Question(What is the array after the first pass of the Bubble Sort algorithm?)

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

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.