Question

In: Computer Science

The following flow chart is a bubble sort. Write the code for this sort. Make sure...

The following flow chart is a bubble sort. Write the code for this sort. Make sure you display each pass and comparison. Include comments.

Solutions

Expert Solution

OUTPUT

CODE FOR BUBBLE SORT

PSEUDOCODE

         n->Number of elements of the array

Step-1          I=0

            Repeat through step 3 while (I<n)

Step-2 j=0

            Repeat through step 3 while j<n-I

Step-3 if arr[j]<arr[j+1]

                 Temp = arr[j]

                 arr[j+1] = arr[j]

                 arr[j]=temp

Step-4 exit

PROGRAM

#include <stdio.h>
#include <stdlib.h>
void main()
{
   int arr[10],i,j,temp; //variable declaration


//loop to input the numbers into array
   for (i = 0; i < 10; i++)
   {
   printf("\nENTER A NUMBER");
   scanf("%d",&arr[i]);
   }
  
   //display elements before sort
   printf("\n Array elements are : \n");
   for (i=0;i<10;i++)
       printf("%5d",arr[i]);
      
//logic for bubble sort
   for (i = 0; i < 10; i++)
       for (j = 0; j < 10-i; j++)
   if (*(arr+j) > *(arr+(j+1))) //compare an element with its next element
           { //swap two numbers
               temp = arr[j];
               arr[j]=arr[j+1];
               arr[j+1]=temp;
           }
//display the sorted list
   printf("\n Sorted list is as follows\n");
   for (i=0;i<10;i++)
           printf("%5d",arr[i]);
}


OUTPUT

EXAMPLE

    


Related Solutions

How would I make a bubble sort and an optimized bubble sort with the code given?...
How would I make a bubble sort and an optimized bubble sort with the code given? I also need to implement a timer into each sort and display runtime with the sorts. NODE.H _______________________________________________________________________________________________________ /* node.h */ /* two classes 1: node.h 2. singlylinkedlist.h nod1 (value + pointer) ---> node2 ---> node3 ---> |||| <--- node.h ^ | singlylinkedlist ----------------*node head; */ #ifndef NODE_H #define NODE_H #include <iostream> using namespace std; class Node {    friend class singlyLinkedList; public:   ...
IN C ++ PLEASE CODE FOR BUBBLE SORT---Add code to sort the bowlers. You have to...
IN C ++ PLEASE CODE FOR BUBBLE SORT---Add code to sort the bowlers. You have to sort their parallel data also. Print the sorted bowlers and all their info . You can use a bubble sort or a shell sort. Make sure to adjust your code depending on whether or not you put data starting in row zero or row one. Sort by Average across, lowest to highest.  The highest average should then be on the last row.. When you sort...
Add bubble sort, radix sort, insertion sort, and merge sort to the code provided. Import a...
Add bubble sort, radix sort, insertion sort, and merge sort to the code provided. Import a data set (txt file) then do the sorting algorithm to measure how long it took and how many movements occurred. Please write codes in C++ Here's data set (should be stored in txt file) 7426 4524 4737 9436 3997 2757 6288 5414 9590 5968 6638 3199 9514 1541 9866 2144 6731 911 2171 6135 6437 912 9417 2662 6606 6349 707 2890 5386 9718...
(code in C++ language) [Code Bubble sort, Insertion sort Create a Big array with random numbers....
(code in C++ language) [Code Bubble sort, Insertion sort Create a Big array with random numbers. Record the time. Run Bubble Check time (compute the processing time) do it 100 times (random numbers) Take the average Insertion: Compare] (some explanations please)
Write in python: Go through the visualization of the Selection Sort, Bubble Sort and Insertion Sort....
Write in python: Go through the visualization of the Selection Sort, Bubble Sort and Insertion Sort. Write a Pseudo code first for each of these sort methods. After writing the pseudo code write python code from the pseudo code.
In Python, there are different sorting algorithms. Selection Sort, Bubble Sort and Insertion Sort. • Write...
In Python, there are different sorting algorithms. Selection Sort, Bubble Sort and Insertion Sort. • Write a Pseudo code first for each of these sort methods.   • After writing the pseudo code write python code from the pseudo code. • Upload the pseudo code and Python code for each of the three algorithm mentioned.
Write Insertion Sort and Bubble Sort Program for C# also write their algorithm and Explain their...
Write Insertion Sort and Bubble Sort Program for C# also write their algorithm and Explain their working.
Write a program and test a program that translates the following Bubble Sort algorithm to a...
Write a program and test a program that translates the following Bubble Sort algorithm to a bubblesort function. The function's prototype is, void bubblesort(int a[], int size); Bubble Sort The inner loop moves the largest element in the unsorted part of the array to the last position of the unsorted part of the array; the outer loop moves the last position of the unsorted part of the array. The Bubble sort exchanges elements with adjacent elements as it moves the...
Sort the following set of numbers using bubble sort, insertion sort, and selection sort. Show the...
Sort the following set of numbers using bubble sort, insertion sort, and selection sort. Show the process step-by-step, and find the time complexity in Big-O notation for each method. For sorting, use ascending order. 49, 7, 60, 44, 18, 105
Write a program to implement and analyzing the Bubble Sort. a. Write a C++ function for...
Write a program to implement and analyzing the Bubble Sort. a. Write a C++ function for Bubble Sort b. Use a dynamic array of integers in a variable size of n. c. Display the following information: 1) Total counts of comparisons 2) Total counts of shifts / moves / swaps, whichever applies d. Write a main() function to test a best, and an average cases in terms of time efficiency i. Fill out the array with random numbers for an...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT