Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.Vector;
- class Student{
- int roll_no;
- String name;
- }
- class SortVec{
- public static void main(String[] args){
- Scanner sc = new Scanner(System.in);
- Vector <Student> v = new Vector();
- System.out.println("Enter 5 Entries.");
- for(int i = 0; i < 5; i++){
- Student s = new Student();
- System.out.print("roll no: ");
- s.roll_no = sc.nextInt();
- System.out.print("name: ");
- s.name = sc.next();
- v.add(s);
- }
- for(int i = 0; i<5; i++){
- int j, temp1 = v.elementAt(i).roll_no;
- String temp2 = v.elementAt(i).name;
- for(j = i-1; j>=0 && temp1 < v.elementAt(j).roll_no; j--){
- v.elementAt(j+1).roll_no = v.elementAt(j).roll_no;
- v.elementAt(j+1).name = v.elementAt(j).name;
- }
- v.elementAt(j+1).roll_no = temp1;
- v.elementAt(j+1).name = temp2;
- }
- for(int i = 0; i < 5; i++){
- System.out.println(v.elementAt(i).roll_no + " " + v.elementAt(i).name);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement