Jump to content

Need help in bubble sort

- - - - -

  • Please log in to reply
2 replies to this topic

#1
arunjib

arunjib

    Learning Programmer

  • Members
  • PipPipPip
  • 76 posts
I want to sort 5 students' marks in decending order. But, I'm unable to display marks according to their name after sorting. please help me to do

import java.io.*;

class q9

{

public static void main(String args[]) throws IOException

{

InputStreamReader read=new InputStreamReader(System.in);

BufferedReader in=new BufferedReader(read);

String n[]=new String[40];

int t[]=new int[5];

int temp, temp2;

for(int i=0;i<5;i++)

{

System.out.print("Enter the name of a student :  ");

n[i]=in.readLine();

System.out.print("Enter total marks of the student :  ");

t[i]=Integer.parseInt(in.readLine());

}

System.out.println("Name          Marks");

for(int i=0;i<5;i++)

System.out.println(n[i]+"            "+t[i]);

for(int i=0;i<4;i++)

{

for(int j=0;j<4-i;j++)

{

if (t[j]<t[j+1])

{

temp=t[j];

t[j]=t[j+1];

t[j+1]=temp;

}

}

}

System.out.println("After sorting...");

System.out.println("Name          Marks");

for(int i=0;i<5;i++)

System.out.println(n[i]+"            "+t[i]);

}

}



#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Just swap the names as well after the marks:

                    temp = marks[j];
                    marks[j] = marks[j + 1];
                    marks[j + 1] = temp;


                    temp2 = names[j];
                    names[j] = names[j + 1];
                    names[j + 1] = temp2;


#3
arunjib

arunjib

    Learning Programmer

  • Members
  • PipPipPip
  • 76 posts
Thnx for reply... done

wim DC said:

Just swap the names as well after the marks:


                    temp = marks[j];

                    marks[j] = marks[j + 1];

                    marks[j + 1] = temp;



                    temp2 = names[j];

                    names[j] = names[j + 1];

                    names[j + 1] = temp2;






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users