Search code examples
c++arraysalgorithmsortingbubble-sort

Bubble Sorting - How to use it?


Im a bit new to programming , Trying to get the idea of Algorithm so i started by Sorting Algorithms.So ive read a lot about it and tried to understand its idea then started by Bubble Sort,But im having a problem in my code , Can someone tell me if im thinking over this correctly ? Im not sure that im still on the right way for this.

EDIT: I want to have the user insert a certain amount of numbers in an array , Then these unarranged numbers to be swapped using the Bubble-Sort.

so here's the code :

#include <iostream>
using namespace std;

int main(){
    int arr[6];
    int temp;
    cout << "Enter an unarranged amount of numbers(6) \n";
    for(int i=0;i<6;i++){
        cin>>arr[i];
    }
    cout << "Normal List : ";
    for(int i=0;i<6;i++){
        cout << arr[i] << " ";
    }
      //Sorting
    for(int i=0;i<6;i++){
        for(int x=0;x=i+1;x++){
            if(arr[i]>arr[x]){
                temp=arr[x];
                arr[x]=arr[i];
                arr[i]=temp;
            }
        }
         cout << arr[i] << " ";
    }
    return 0;
}

Solution

  • This loop

    for(int x=0;x=i+1;x++){
    

    is an infinite loop because in the condition of the loop there is used the assignment

    x=i+1
    

    So the value of x that is the value of the condition is never will be equal to 0.

    And the bubble sort algorithm compares and swaps adjacent elements of an array.

    Also you could use the standard function std::swap to swap elements of the array.

    The loops that implement the bubble sort can look for example the following way as it is shown in the demonstrative program below

    #include <iostream>
    #include <utility>
    
    int main() 
    {
        const size_t N = 6;
    
        int arr[N];
    
        std::cout << "Enter an unarranged amount of " << N << " numbers: ";
    
        for ( auto &item : arr ) std::cin >> item;
    
        std::cout << "Before sorting: ";
        for ( const auto &item : arr ) std::cout << item << ' ';
        std::cout << '\n';
    
        for ( size_t n = N, last = n; !( n < 2 ); n = last )
        {
            for ( size_t i = last = 1; i < n; i++ )
            {
                if ( arr[i] < arr[i-1] )
                {
                    std::swap( arr[i], arr[i-1] );
                    last = i;
                }
            }
        }
    
        std::cout << "After  sorting: ";
        for ( const auto &item : arr ) std::cout << item << ' ';
        std::cout << '\n';
    
        return 0;
    }
    

    Its output might look for example like

    Enter an unarranged amount of 6 numbers: 6 3 1 2 4 5
    Before sorting: 6 3 1 2 4 5 
    After  sorting: 1 2 3 4 5 6 
    

    As for your code then the inner loop should look at least like

        for(int x = 1; x < 6; x++ ){
            if ( arr[x-1] > arr[x] ){
                temp=arr[x];
                arr[x]=arr[x-1];
                arr[x-1]=temp;
            }
        }
    

    and remove this statement after the loop

    cout << arr[i] << " ";