Help needed in a simple sort programme

Here's the question :-
You will be given several lines of input where each line contains a sequence of positive integers. Your task is to sort these lines in lexicographic order. Lexicographic order is the order in which words are listed in the dictionary. The ordering is determined by the left most element (in this case the left most integer on each line) and if the left most elements are equal then by the second element from the left, if the left most and the second element from the left are equal then by the third element from the left and so on.
For example, when the lines
14 38 11 89
27 34
27 12 34
27
92 2 3 1
17 2
are sorted in the lexicographic order we get
14 38 11 89
17 2
27
27 12 34
27 34
92 2 3 1
Input format
The first line of the input contains a single integer N indicating the number of lines of input. This is followed by N lines (lines 2 through N+1) each which consists of a sequence of positive integers followed by a single -1. (The -1 is there just as an end marker and is not to be used in the sorting). Every line contains at the most 50 numbers.
Output format
N lines each containing a sequence of integers. These N lines must be the lexicographically sorted presentation of the N input lines.
Test data
You may assume that N ≤ 1000. (Recall that there are at the most 50 integers on each line).
Example
We now illustrate the input and output formats using the above example:
Sample input:
6
14 38 11 89 -1
27 34 -1
27 12 34 -1
27 -1
92 2 3 1 -1
17 2 -1
Sample output:
14 38 11 89
17 2
27
27 12 34
27 34
92 2 3 1


And here's my solution :-
#include<iostream>
using namespace std;

int i,j,k=0,n,num[51][51];

void shiftit (int a,int b) {
int tmp[50],x,y,p_1,p_2;
for (x=0;x<n;x++) {
if (num[x][50]==a) p_1=x;
if (num[x][50]==b) p_2=x;
}
for (x=0;;x++) {tmp[x]=num[p_1][x];if (num[p_1][x]==-1) break;}
for (x=0;x<50;x++) {
num[p_1][x]=num[p_2][x];
num[p_2][x]=tmp[x];
}
}

int main() {
cin>>n;
for (i=0;i<n;i++) {for (j=0;;j++) {cin>>num[i][j];if (num[i][j]==-1) break;}num[i][50]=i;}
for (i=0;i<n-1;i++) {
for (j=i+1;j<n;j++) {
victory:
if (num[j][k]<num[i][k]) {
shiftit(num[i][k],num[j][k]);
}
if (num[j][k]==num[i][k]) {
k++;
goto victory;
}
}
}
for (i=0;i<n;i++) {for (j=0;;j++) {cout<<num[i][j]<<" ";if (num[i][j]==-1) break;}cout<<endl;}
system("PAUSE");
}


It is giving wrong answers obviously i know that fault is from Line having
victory:
but dont know whats wrong and neither am able to correct this simple solution's problem , pls help !
Hi, your post is unreadable... Please format ur code with proper indentation and code blocks like this:
1
2
3
4
5
6
7
for(int i; i<10; i++)
{
   for(int j; j<10; j++)
   {
      cout << "Hello?";
   }
}


A few points:
int main() should return something
'goto' is bad as are calls to system()
Last edited on
Topic archived. No new replies allowed.