Search code examples
c++arrayssortingbubble-sort

Bubble sort the numbers from a file in descending order for the first number


I want to sort using the "Bubble Sort" algorithm of the 2d array. My array size will be about array[100000][100000]. my input number will be n=100,000.

For now we can use a small size of the array to fix the sorting issue.

  1. I need to sort them in descending order for the first number(first number's line).
  2. If the first number of 2 values are the same, then I have to sort them according to their second number.
  3. Finally I have to output the result into a txt file

Let's' understand using an example. Here, my input looks like this

41 11
34 4
69 4
78 6
62 8
5 5
81 3
5 10

above our input example and we have a couple of inputs. Now I need to sort them descending orders for the first number. But if the first number of 2 values are the same, then sort them according to their second number.

Example output below,

81 3
78 6
69 4
62 8
41 4
34 4
5 10
5 5

If anyone can please help me.

I am a beginner so I am trying to input the file manually to solve this sorting problem. I can solve the sorting problem then I will try to input and out the text.

Something I have tried but not worked. I am still trying to solve it.

#include<bits/stdc++.h>
#include <algorithm>
using namespace std;

int main ()
{
    int arr[100][100];
    int n,j;
    cin >>n;
    cout << "Please enter a number: " << endl;
    for(int i=0;i<n;i++)
    {   for (int j=i; j<n; j++)
        {
            cin>>arr[i][j];
        }
    }
    cout << "Unsorted array:" << endl;
    for (int i=0; i<n; i++)
    {
        for (int j=i; j<n; j++)
        {
             cout<<arr[i][j]<<"\t";
        }

    }

    for (int i=0; i<=n; i++)
    {
        for (int j=i+1; j<=n-1; j++)
        {
            int temp;
            if(arr[i]>arr[j])
            {
                temp=arr[i];
                arr[i]=arr[j];
                arr[j]=temp;
            }
        }
    }
return 0;
}

Solution

  • Use a std::vector<std::array<int,2>>for your base container. The dynamic growth capabilities of std::vector solves your stack space issue, and the std::array use gives you tied cell comparison. I.e. you can do this:

    std::array<int, 2> ar1{1,2}, ar2{1,3};
    
    if (ar1 < ar2) ...
    

    and it will do the right thing. The result then boils down to effectively this:

    #include <iostream>
    #include <array>
    #include <vector>
    #include <utility>
    
    int main()
    {
        std::vector< std::array<int,2> > v;
        std::size_t n;
        if (std::cin >> n && n > 0)
        {
            std::array<int,2> row;
            while (n-- && std::cin >> row[0] && std::cin >> row[1])
                v.emplace_back(row);
    
            // bubblesort the content
            std::size_t len = v.size();
            while (len-- > 0)
            {
                bool swapped = false;
                for (std::size_t i=0; i<len; ++i)
                {
                    // std::array support multi-cell comparison.
                    if (v[i] < v[i+1])
                    {
                        // use library swap to swap entire row.
                        std::swap(v[i], v[i+1]);
                        swapped = true;
                    }
                }
    
                // early exit if no swaps happened on the last pass
                if (!swapped)
                    break;
            }
    
            // report final output.
            for (auto const& row : v)
                std::cout << row[0] << ' ' << row[1] << '\n';
        }
    }
    

    Input

    8
    41 11
    34 4
    69 4
    78 6
    62 8
    5 5
    81 3
    5 10
    

    Output

    81 3
    78 6
    69 4
    62 8
    41 11
    34 4
    5 10
    5 5