/* Wondered how we can do sort an array without Shifting or Swapping ? Well, here it is: */
import java.util.*;
class sorting_without_shifing_or_swapping
{
void main()
{
Scanner in = new Scanner(System.in);
int array[] = new int[5];
for(int v = 0 ; ; )
{
System.out.println("Enter the Array" ); //I am assumung that the array is of length = 5
for(int i = 0 ; i<5 ; i++) // inputting the array
{
array[i] = in.nextInt();
}
int largest = array[0], small = array[0];
for(int j = 0 ; j<5 ; j++)// a loop to find the smallest and the largest element
{
if(largest<array[j])
{
largest = array[j];
}
if(small>array[j])
small = array[j];
}
System.out.println("The sorted array :");
for(int i = small ;i<=largest ; i++) //Here is the magic!!!
{
for(int k = 0 ; k<5 ; k++)
{
if(i==array[k])
{
System.out.println(array[k]);
}
}
}
System.out.println("If you want to EXIT enter 1 else enter any other number");
int ch = in.nextInt();
if(ch==1)
{
break;
}
}
}
}
- SHASHWAT SRIVASTAVA
MY OUTPUT |
import java.util.*;
class sorting_without_shifing_or_swapping
{
void main()
{
Scanner in = new Scanner(System.in);
int array[] = new int[5];
for(int v = 0 ; ; )
{
System.out.println("Enter the Array" ); //I am assumung that the array is of length = 5
for(int i = 0 ; i<5 ; i++) // inputting the array
{
array[i] = in.nextInt();
}
int largest = array[0], small = array[0];
for(int j = 0 ; j<5 ; j++)// a loop to find the smallest and the largest element
{
if(largest<array[j])
{
largest = array[j];
}
if(small>array[j])
small = array[j];
}
System.out.println("The sorted array :");
for(int i = small ;i<=largest ; i++) //Here is the magic!!!
{
for(int k = 0 ; k<5 ; k++)
{
if(i==array[k])
{
System.out.println(array[k]);
}
}
}
System.out.println("If you want to EXIT enter 1 else enter any other number");
int ch = in.nextInt();
if(ch==1)
{
break;
}
}
}
}
- SHASHWAT SRIVASTAVA
No comments:
Post a Comment