I will explain very simply: there are 2 columns and 5,000 lines, in 1 column there are numbers from 0 to 4999, in the second there are random numbers. Here is an example:

[0][42] [1][23] [2][44] [3][123] [4][80] [5][19] 

It is necessary to arrange the second column so that the numbers in the first match the second. Here is an example:

 [5][19] [1][23] [0][42] [2][44] [4][80] [3][123] 

I need this for a binary search.

  • “that the numbers in the first match the second” is not entirely clear. Is a two-dimensional array straight? Why not use set, which stores an object - two pairs of numbers + sorting function? - Sublihim
  • So I asked the teacher, it is necessary that when searching for elements by the linear and binary method, the numbers have 1 and the same index - Tark
  • you skip the lectures, and we solve the problems for you? :) - Sublihim
  • I do not skip lectures, just practice and lectures go to discord, I can sort arrays, but only completely, and not whole lines according to the values ​​of one column. And I already have a programming machine, I just help a friend, they give everyone different protection. - Tark
  • @Tark I did not understand. Do you know human language? What do you need to do? Sort matrix rows by the values ​​in the second column? - Vlad from Moscow

3 answers 3

If you must use your own sorting method of choice, then its code may look like this

 for ( size_t i = 0; i < N; i++ ) { size_t min = i; for ( size_t j = i + 1; j < N; j++ ) { if ( mas[j][1] < mas[min][1] ) { min = j; } } if (min != i) { int tmp[2] = { mas[i][0], mas[i][1] }; mas[i][0] = mas[min][0]; mas[i][1] = mas[min][1]; mas[min][0] = tmp[0]; mas[min][1] = tmp[1]; } } 

type size_t can be replaced by type int , if the variable N is of type int .

  • Thank you very much, everything works fine) - Tark

First define the comparison function.

 bool cmp(int[] a, int[] b) { return a[1] < b[1]; } 

Then sort

 std::sort(arr.begin(), arr.end(), cmp); 

C ++ is not my main language, I could have written something wrong from memory.


 for (i=1;i<N;i++) { x=mas_order[i]; j=i-1; while (j>=0 && x[1]<mas_order[j][1]) { mas_order[j+1]=mas_order[j]; j=j-1; } mas_order[j+1]=x; } 
  • everything would be fine, but we have not yet been trained in functions, and this will be very noticeable. - Tark
  • @Tark then give the code that you use to sort the regular array, I will correct it - RusArt
  • Like it is indicated that it is necessary to "Sort a two-dimensional array" - Sublihim
  • 'for (i = 1; i <N; i ++) {x = mas_order [i]; j = i-1; while (j> = 0 && x <mas_order [j]) {mas_order [j + 1] = mas_order [j]; j = j-1; } mas_order [j + 1] = x; } - Tark
  • I do not understand how to correctly insert the code, but the sorting is done by the method of inclusion - Tark

It turned out something like this but it still doesn’t work, it sorts the 2nd column and in the first it writes to the second column. keeping the index of the number of the second column, but does not work, I don’t know what to do with this two-dimensional array (

 int Nmin,min1,min2; for (int i=0;i<N-1;i++) { Nmin=i; min1=mas[i][1]; min2=mas[i][0]; for (int j=i+1;j<N;j++) { if (min1>mas[j][1]) { min1=mas[j][1]; min1=mas[j][0]; Nmin=j; } } mas[Nmin][1]=mas[i][1]; mas[Nmin][0]=mas[i][0]; mas[i][1]=min1; mas[i][0]=min2; }