0

i want to use a bubble sort to a external data file while looks like this.

00000002

45

56

32

46

00000001

87

95

83

100

so all the data that is under 00000001 appear before 00000002. i'm guessing in order to do so, i need to use a 3d array. is there is a way a way to store all the data under 00000001 under a variable and just use a simple bubble sort method to sort it. if this can be done, then i could use something like this to sort.

void displayArray(int sArray[2])
{
    int i = 0;
    while(i<2)
    {         
        cout << sArray[i];
        cout << "\n";
        i++;
    }    
}

void sortArray(int sArray[2])
{
    int temp = 0;
    bool sorted = false;

    while(sorted == false)
    {
        sorted = true;
        for(int i=0; i<1;i++)
        {
            if(sArray[i]>sArray[i+1])
            {
                temp = sArray[i];
                sArray[i] = sArray[i+1];
                sArray[i+1] = temp;
                sorted = false;
            }
        }
    }
}
Diljot Singh
  • 1
  • 1
  • 1
  • 1
    umm, your array has only two elements, that's not make sense – DGomez Jun 14 '13 at 03:33
  • Sorry, why exactly do you need a three-dimensional array? It sounds like you have a few numbers and want to sort them. A single dimension should be enough for that..? – jogojapan Jun 14 '13 at 03:34
  • im trying to get the bottom 5 values on top using a bubble sort. – Diljot Singh Jun 14 '13 at 03:36
  • One way of doing this is is to put all numbers into one (ordinary, 1D) array, sort it, and then pick the first five elements. – jogojapan Jun 14 '13 at 03:37
  • oh i see. that makes sense. so i just need to use a 1D array and sort array[0] with array[5] and then just pick the rest of the values. – Diljot Singh Jun 14 '13 at 03:47
  • im sorry. i got confused with 2d and 3d. it makes sense now. thank you. – Diljot Singh Jun 14 '13 at 03:48
  • Wrap it in a [flattening iterator](https://stackoverflow.com/questions/3623082/flattening-iterator) and call `std::sort` – Caleth Feb 07 '18 at 10:56

2 Answers2

0

Whatever sorting algorithm you use you should use pointers to the first and last position. In this case you can sort items 1-4 and then run the sort on items 6-10.

Seth Hays
  • 311
  • 2
  • 11
0

This function is made for bubble sorting for 3D array in ascending order.

If you want to arrange in descending order so just change > instead of <

If you want change the size of array then you would change the loop size.

#include <iostream>
using namespace std;

int sorting(int array[2][2][5]){
     int temp;
     for (int i = 0 ; i < 2 ; i++ )
         for (int j = 0 ; j < 2; j++ )
             for (int k = 0 ; k < 5 ; k++ )
                for (int a = 0 ; a < 2 ; a++ )
                    for (int b = 0 ; b < 2 ; b++ )
                        for (int c = 0 ; c < 5 ; c++ )
                            if (array[i][j][k] < array[a][b][c]){
                                temp = array[i][j][k];
                                array[i][j][k] = array[a][b][c];
                                array[a][b][c] = temp;}
   for (int i = 0 ; i < 2 ; i++ )
       for (int j = 0 ; j < 2 ; j++ )
           for (int k = 0 ; k < 5 ; k++ ){
               cout << array[i][j][k] << endl;
           }
    return 0;
}
int main(){
    int array1[2][2][5] = {{{45,23,67,1,-12},{34,43,234,567,576}},
    {{345,345,76,87,2},{456,789,890,32,4}}};
     sorting(array1);
}
////This is example of bubble sorting of 3D array. 
il_raffa
  • 5,090
  • 129
  • 31
  • 36
dextoruz
  • 11
  • 5
  • Please provide some relevant explanations to your answer as well. – BlackBeard Feb 07 '18 at 10:29
  • This function is made for bubble sorting for 3D array in ascending order. If you want to arrange in descending order so just change ">" instead of "<". And if you want change the size of array then you would change the loop size. – dextoruz Feb 07 '18 at 15:28