selection sorting with vector in java

it is copiled properly but output of selction sort in not coming

Page 1 of 1

4 Replies - 649 Views - Last Post: 04 October 2012 - 08:51 AM Rate Topic: -----

#1 friendsworld89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 10-September 09

selection sorting with vector in java

Posted 14 September 2009 - 07:53 AM

import java.util.*;
import java.io.*;
import java.lang.*;

public class StringSelect 
{
public static void main(String args[])throws IOException
{
		Vector b=new Vector();
		 
		 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		 
		 System.out.print("\nEnter the how many numbers you want to sorted :");
	   
		 int num=Integer.parseInt(br.readLine());
		 System.out.println();
		
		 for(int i=0;i<num;i++)
		{
			   System.out.println("Enter No."+ (i+1));
			   b.addElement(br.readLine());
			   System.out.println();
		}	  
			   System.out.print("The Vector values before Sorting   ");
			  for(int i=0;i<b.size();i++)
			  {
				   System.out.print(b.elementAt(i)+" ");
			  }
	  System.out.println("\n");
	  System.out.println("Menu");
	  System.out.println("1.BubbleSort");
	  System.out.println("2.SelectionSort");
	  System.out.println("3.InsertionSort");
	  System.out.println("4.MergeSort");
	  System.out.println();
	  System.out.print("\nEnter the the Option :");
	  int ans=Integer.parseInt(br.readLine());
	  System.out.println();
	  switch(ans)
	  {
		   case 1:
					   System.out.println("The vector Element After Sorting :\n");
					   
					   boolean swapped = true;
						
					   while(swapped){
											  swapped=false;
											  for(int i=0;i<b.size()-1;i++)
											  {
													   String str1 = (String) b.elementAt(i);
				int numOne = Integer.parseInt(str1);
				String str2 = (String) b.elementAt(i + 1);
				int numTwo = Integer.parseInt(str2);

				if(numOne > numTwo) 
				{
	 					b.setElementAt(str2, i);
						b.setElementAt(str1, i+1);
														   swapped=true;
				}

											  }
							 
										  }
					   for(int i=0;i<b.size();i++)
					  { 
		System.out.print(b.elementAt(i)+"  ");
			}

					 System.out.println();
					 break;

		 case 2:
		System.out.println("The vector Element After Sorting :\n");
							for(int i=0;i<b.size()-1;i++)
							{
									 int min=i;
										 for(int j=i+1;j<b.size()-1;j++)
										 {
													   String str1 = (String)	  b.elementAt(i);
				int numOne = Integer.parseInt(str1);
				String str2 = (String) b.elementAt(j);
				int numTwo = Integer.parseInt(str2);

				if(numOne > numTwo)
				{
															min=j;   
				}
										 }
								   if(i != min)
								   {
				String temp=(String) b.elementAt(i);
								   b.setElementAt(b.elementAt(min),i);
								   b.setElementAt(temp,min);
								   }
							 }
	
						
										 for(int i=0;i<b.size();i++)
										{ 
						  System.out.print(b.elementAt(i)+"  ");
							   }

										 System.out.println();
										 break;
											  
		}
		  

	 }
}




/*can anyone tell what is wrong in my program in case 2 to help me in sorting with selection sort . compiler is not showing any error but the sorting not getting...*/

This post has been edited by friendsworld89: 14 September 2009 - 10:20 AM


Is This A Good Question/Topic? 0
  • +

Replies To: selection sorting with vector in java

#2 shweta.maniwade  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 04-October 12

Re: selection sorting with vector in java

Posted 04 October 2012 - 08:04 AM

View Postfriendsworld89, on 14 September 2009 - 07:53 AM, said:

import java.util.*;
import java.io.*;
import java.lang.*;

public class StringSelect 
{
public static void main(String args[])throws IOException
{
		Vector b=new Vector();
		 
		 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		 
		 System.out.print("\nEnter the how many numbers you want to sorted :");
	   
		 int num=Integer.parseInt(br.readLine());
		 System.out.println();
		
		 for(int i=0;i<num;i++)
		{
			   System.out.println("Enter No."+ (i+1));
			   b.addElement(br.readLine());
			   System.out.println();
		}	  
			   System.out.print("The Vector values before Sorting   ");
			  for(int i=0;i<b.size();i++)
			  {
				   System.out.print(b.elementAt(i)+" ");
			  }
	  System.out.println("\n");
	  System.out.println("Menu");
	  System.out.println("1.BubbleSort");
	  System.out.println("2.SelectionSort");
	  System.out.println("3.InsertionSort");
	  System.out.println("4.MergeSort");
	  System.out.println();
	  System.out.print("\nEnter the the Option :");
	  int ans=Integer.parseInt(br.readLine());
	  System.out.println();
	  switch(ans)
	  {
		   case 1:
					   System.out.println("The vector Element After Sorting :\n");
					   
					   boolean swapped = true;
						
					   while(swapped){
											  swapped=false;
											  for(int i=0;i<b.size()-1;i++)
											  {
													   String str1 = (String) b.elementAt(i);
				int numOne = Integer.parseInt(str1);
				String str2 = (String) b.elementAt(i + 1);
				int numTwo = Integer.parseInt(str2);

				if(numOne > numTwo) 
				{
	 					b.setElementAt(str2, i);
						b.setElementAt(str1, i+1);
														   swapped=true;
				}

											  }
							 
										  }
					   for(int i=0;i<b.size();i++)
					  { 
		System.out.print(b.elementAt(i)+"  ");
			}

					 System.out.println();
					 break;

		 case 2:
		System.out.println("The vector Element After Sorting :\n");
							for(int i=0;i<b.size()-1;i++)
							{
									 int min=i;
										 for(int j=i+1;j<b.size()-1;j++)
										 {
													   String str1 = (String)	  b.elementAt(i);
				int numOne = Integer.parseInt(str1);
				String str2 = (String) b.elementAt(j);
				int numTwo = Integer.parseInt(str2);

				if(numOne > numTwo)
				{
															min=j;   
				}
										 }
								   if(i != min)
								   {
				String temp=(String) b.elementAt(i);
								   b.setElementAt(b.elementAt(min),i);
								   b.setElementAt(temp,min);
								   }
							 }
	
						
										 for(int i=0;i<b.size();i++)
										{ 
						  System.out.print(b.elementAt(i)+"  ");
							   }

										 System.out.println();
										 break;
											  
		}
		  

	 }
}




/*can anyone tell what is wrong in my program in case 2 to help me in sorting with selection sort . compiler is not showing any error but the sorting not getting...*/

Was This Post Helpful? 0
  • +
  • -

#3 shweta.maniwade  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 04-October 12

Re: selection sorting with vector in java

Posted 04 October 2012 - 08:09 AM

import java.util.*;
import java.io.*;
import java.lang.*;
public class StringSelect1
{
	static int j,y;
	public static void main(String args[])throws IOException
	{
	 	Vector b=new Vector();
	         	BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
	         	System.out.print("\nEnter the how many numbers you want to sorted :");
		int num=Integer.parseInt(br.readLine());
		System.out.println();
		for(int i=0;i<num;i++)
	         	{
	               		System.out.println("Enter No."+ (i+1));
	               		b.addElement(br.readLine());
	               		System.out.println();
	        	}    
	               	System.out.print("The Vector values before Sorting:");
	              	for(int i=0;i<b.size();i++)
	             	{
	                   	System.out.print(b.elementAt(i)+" ");
	              	}
	 
	      	System.out.println("\n");
	      	System.out.println("Menu");
	      	System.out.println("1.BubbleSort");
	      	System.out.println("2.SelectionSort");
	      	System.out.println("3.InsertionSort");
	      	System.out.println("4.MergeSort");
	     	System.out.println();
	      	System.out.print("\nEnter the the Option :");
	      	int ans=Integer.parseInt(br.readLine());
	      	System.out.println();
	      	switch(ans)
	      	{
	           		case 1:
	                 	boolean swapped = true;
	                   	while(swapped)
			{
				swapped=false;
	                                              	for(int i=0;i<b.size()-1;i++)
	                                              	{
	                                                 	String str1 = (String) b.elementAt(i);
	                                                 	int numOne = Integer.parseInt(str1);
	                                                       	String str2 = (String) b.elementAt(i + 1);
	                                                       	int numTwo = Integer.parseInt(str2);
				    	if(numOne > numTwo)
	                                		{
	                                                    		b.setElementAt(str2, i);
	                                                   		b.setElementAt(str1, i+1);
	                                                    		swapped=true;
	                                                	}
	 
	                                              	}
	                              
	                        	}
	                       	System.out.println("\n1.Ascending Sorting");
	             		System.out.println("\n2.Descending Sorting");
	                       	System.out.println("\nEnter the option");
	                       	int t=Integer.parseInt(br.readLine());
	                       	if(t==1)
	                       	{
	                       		System.out.println("The vector Element After Sorting :\n");
	                       		for(int i=0;i<b.size();i++)
	                      		{
	        				System.out.print(b.elementAt(i)+"  ");
	            			}
	 
	                        		System.out.println();
	                        		break;
	                        	}
	                       	else
	                        	{
	                       		System.out.println("The vector Element After Sorting :\n");
	                       		for(int i=(b.size()-1);i>=0;i--)
	                      		{
	        				System.out.print(b.elementAt(i)+"  ");
	            			}
	 
	                        		System.out.println();
				break;
	                        	}
			
	              	 	case 2:
			for(int i=0;i<b.size();i++)
	                            	{     
	                                 	int min=i;
	                                        	 for(int j=i+1;j<b.size();j++)
	                                         	{
	                                                	String str1 = (String) b.elementAt(min);
	                			int numOne = Integer.parseInt(str1);
	                			String str2 = (String) b.elementAt(j);
	                			int numTwo = Integer.parseInt(str2);
	 
	                			if(numOne > numTwo)
	                			{
	                                    			min=j;  
	                			}
	                                         	}
	                                   	if(i != min)
	                                   	{         
	                                        		String temp=(String) b.elementAt(i);
	                                       		 b.setElementAt(b.elementAt(min),i);
	                                        		b.setElementAt(temp,min);
	                                   	}
	                             	}
	 		System.out.println("\n1.Ascending Sorting");
	             		System.out.println("\n2.Descending Sorting\n");
	                       	System.out.println("\nEnter the option :");
	                       	int k=Integer.parseInt(br.readLine());
	                       	if(k==1)
	                       	{
	                       		System.out.println("\nThe vector Element After Sorting :\n");
	                       		for(int i=0;i<b.size();i++)
	                      		{
	        				System.out.print(b.elementAt(i)+"  ");
	            			}
	 
	                        		System.out.println();
	                        		break;
	                        	}
	                       	else
	                        	{
	                       		System.out.println("\nThe vector Element After Sorting :\n");
	                      		 for(int i=(b.size()-1);i>=0;i--)
	                      		{
	        				System.out.print(b.elementAt(i)+"  ");
	            			}
	 
	                        		System.out.println();
	                        		break;
	                      	}
	       		case 3:
	                         	System.out.println("The vector Element After Sorting :\n");
	                          	int i,index;
	                          
	                         	 for(i=1;i<b.size()-1;i++)
	        		{
				String e1,e2;
	                                   	String str1=(String) b.elementAt(i);
	                                    	index= Integer.parseInt(str1);
	                                    	int x=i;
	                                  	 j=i;
	                                   	String str3= (String) b.elementAt(j);
	                                   	int m=Integer.parseInt(str3);
	                                   	String str2 = (String) b.elementAt(j-1);
	                                   	y= Integer.parseInt(str2);
	                                   	int n=(j-1);
				e1=""+b.elementAt(j);
				e2=""+b.elementAt(i);
	 
	                 		 while ((y>0)&&(e1.compareTo(e2))>0)
	                                     	{
	                                            		b.setElementAt(b.elementAt(j),n);
	                                             
	                                            		 j--;
	                                     	}
	                                             
	                                            	b.setElementAt(b.elementAt(j),x);
	                             	}
	                            
	 
	                                for(i=0;i<b.size();i++)
	                               {
	                               		System.out.print(b.elementAt(i)+"  ");
	                               }
	 
	                                         System.out.println();
	                                         break;
	 
	                                               
	        	}
	           
	 
	  }
}
/*

u cant compare one element with the other ..in case 3 u used compare to to compare one element with d other but the right method is that store d elemnts in variables el n e2..its compiles perfectly!!!

This post has been edited by Dogstopper: 04 October 2012 - 08:53 AM
Reason for edit:: USE CODE TAGS

Was This Post Helpful? 0
  • +
  • -

#4 Kinaces  Icon User is offline

  • D.I.C Head

Reputation: 78
  • View blog
  • Posts: 230
  • Joined: 04-October 12

Re: selection sorting with vector in java

Posted 04 October 2012 - 08:43 AM

I am questioning your choice of using vectors.
I would suggest looking at another Collection to use. Vectors are made to be thread safe, so unless you are using them for that purpose I do not suggest them. You could use a plain array, or ArrayList, or LinkedList.

ALSO: If you do choose to use a Collection (EX: Vector, ArrayList, LinkedList) you do need to put them in this format.
EX: Vector<E> foo = new Vector<E>(); (Where E is the type of Object you are holding in this Collection). This will get rid of the Unsafe Operation warning when you compile.

Another possibility.. Since you are dealing with numbers. Why not use a Scanner or DataInputStream? That way you don't have to convert everything from Strings.

As for your algorithm. This is your problem.

				int min=i;
				for(int j=i+1;j<b.size()-1;j++)
				{
					String str1 = (String)b.elementAt(i);
					int numOne = Integer.parseInt(str1);
					String str2 = (String) b.elementAt(j);
					int numTwo = Integer.parseInt(str2);

					if(numOne > numTwo)
					{
						min=j;
					}
				}



Inside this inner for loop you are comparing the 2 numbers like you are doing bubble sort. Selection sort works by finding the lowest value and switching it with the value at index i. numTwo is not needed.
Was This Post Helpful? 1
  • +
  • -

#5 Kinaces  Icon User is offline

  • D.I.C Head

Reputation: 78
  • View blog
  • Posts: 230
  • Joined: 04-October 12

Re: selection sorting with vector in java

Posted 04 October 2012 - 08:51 AM

Another note. On that inner for loop you are comparing min to the length of the vector MINUS one. So the last index is never going to be sorted since min will never compare to the last index.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1