Question

I don't understand how the outter loop decrementing i, setting j=i, and j incrementing. Why is...

I don't understand how the outter loop decrementing i, setting j=i, and j incrementing. Why is this nested for loop and j=i necessary? What is happening here?

void bubble_sort() {
#define ARRAYSIZE 12
int array[ARRAYSIZE] = { 5, 7, 99, 12, 42, 31, 1, 23, 9, 21, 22, 45 };

for (int i = ARRAYSIZE - 2; i > 0; i--) {
for (int j = i; j < ARRAYSIZE - 1; j++) {
if (array[j] > array[j + 1]) {
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
} else {
break;
}
}
}

private_sort_check(array);
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The given code is trying to sort the arrays in the order but the code will not sort the array in proper order..This is not the proper code to perform the bubble sort

#include <iostream>

using namespace std;

void bubble_sort() {

#define ARRAYSIZE 12

int array[ARRAYSIZE] = { 5, 7, 99, 12, 42, 31, 1, 23, 9, 21, 22, 45 };

int temp;

for (int i = 0; i < ARRAYSIZE-1; i++)

for (int j = 0; j < ARRAYSIZE-i-1; j++)

if (array[j] > array[j+1]){

temp = array[j+1];

          array[j+1] = array[j];

           array[j] = temp;

}

for(int i=0;i<ARRAYSIZE;i++)

cout<<array[i]<<" ";

}

int main() {

bubble_sort();

}

Add a comment
Know the answer?
Add Answer to:
I don't understand how the outter loop decrementing i, setting j=i, and j incrementing. Why is...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I don't know how to run this code on visual studios and I don't understand why...

    I don't know how to run this code on visual studios and I don't understand why the answer is 211111118. Please explain! Thank you! CONSIDER THE FOLLOWING CODE: void Question() {       string x = "12";       mystery1(x);       string str1 = "21";       mystery2(str1);       string str2 = "11";       mystery2(str2);       string str3 = "31";       mystery2(str3);       string str4 = "91";       mystery2(str4);       string str5 = "81";       mystery1(str5);       x = x + str1 +...

  • I need help of how to include the merge sort code into this counter code found...

    I need help of how to include the merge sort code into this counter code found below: #include<iostream> using namespace std; int bubble_counter=0,selection_counter=0; // variables global void bubble_sort(int [], int); void show_array(int [],int); void selectionsort(int [], int ); int main() { int a[7]={4,1,7,2,9,0,3}; show_array(a,7); //bubble_sort(a,7); selectionsort(a,7); show_array(a,7); cout<<"Bubble counter = "<<bubble_counter<<endl; cout<<"Selection Counter = "<<selection_counter<<endl; return 0; } void show_array(int array[],int size) { for( int i=0 ; i<size; i++) { cout<<array[i]<< " "; } cout<<endl; } void bubble_sort( int array[],...

  • I posted this question earlier but realized that the code was not easy to test since...

    I posted this question earlier but realized that the code was not easy to test since it was just screenshots and not text so I am reposting it: I'm very new at working with file streams, and I'm having trouble with a HW assignment I have to work on. For the assignment I have to read an input file named "MagicSquaresIn.txt" (included at the bottom) and I have to check whether they are normal, associative, and panmagic. To check that...

  • Please help me with this Java project. I'm trying to create a loop so if user...

    Please help me with this Java project. I'm trying to create a loop so if user enter value > 12, will prompt them a message and take them back to "How many elements do you wan to enter?". Thanks public static void main(String[] args) {        Scanner sc = new Scanner(System.in);           int i, j;       System.out.print("\n How meny elements do you want to enter? (N should be no more than 12) ");    int num...

  • Consider the following function bubbleSort. int bubbleSort(int num[], int numel) {       int i, j, temp,...

    Consider the following function bubbleSort. int bubbleSort(int num[], int numel) {       int i, j, temp, moves = 0;       for (i = 0; i < (numel - 1); i++)       {             for (j = 1; j < numel; j++)             {                   //missing code             }       } } A. Complete the missing code:- (4 Points) B. Assume that the array sort[] which has 8 elements, initially contains the values:- 22 5 67 98 45 32 101 99...

  • I need help finishing this question up: I understand I need to add a destructor for...

    I need help finishing this question up: I understand I need to add a destructor for aptr. I am puzzled by the rest template <class T> void SimpleVector<T>::push_back(T element){    // Allocate a new array 1 element larger than the current one.    T* aptrp1 = new T[arraySize + 1];    // Copy the current array contents to the new array    for (int i = 0; i < arraySize; i++) { aptr[i] = aptrp1[i]; }    // Copy the...

  • How would I be able to get a Merge Sort to run in this code? MY...

    How would I be able to get a Merge Sort to run in this code? MY CODE: #include <iostream> #include <fstream> #include <stdlib.h> #include <stdio.h> #include <time.h> using namespace std; class mergersorter { private:    float array[1000] ;    int n = 1000;    int i=0; public:    void fillArray()    {        for(i=1;i<=n;i++)        {            array[i-1]= ( rand() % ( 1000) )+1;        }    }    void arrayout ()    {   ...

  • Hello this is my java sorting algorithm program i need all of my errors corrected so...

    Hello this is my java sorting algorithm program i need all of my errors corrected so I can run it. thank you!! import java.util.Scanner;    public class SortingAlogs { public static void main(String[]args){ int array [] = {9,11,15,34,1}; Scanner KB = new Scanner(System.in); int ch; while (true) { System.out.println("1 Bubble sort\n2 Insertion sort\n3 Selection sort\n"); ch = KB.nextInt(); if (ch==1)    bubbleSort(array); if (ch==2)    insertion(array); if (ch==3)    Selection(array); if (ch==4) break;    print(array);    System.out.println(); }    }...

  • Hello, I need help with my code. The code needs to display random number from 1...

    Hello, I need help with my code. The code needs to display random number from 1 to 50 every time the program runs but the program displays the same random numbers every time. Thanks #include #include using namespace std; void dynAlloc(int size, int *&arr); void displayArray(int *arr, int n); void insertionSort(int *arr, int n, int *temp); void linear_search(int *arr, int n, int key); void binary_search(int *arr, int n, int key); int main() {   const int n = 50; int *arr,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT