Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- class Q_9{
- public static void main( String args[] ){
- System.out.print('\u000C');
- Scanner sc = new Scanner( System.in );
- System.out.println("Enter the size of the array:");
- int size = sc.nextInt();
- if( size <= 0 ){
- System.out.println("Invalid size. Value must more than 0");
- System.exit(0);
- }
- int data[] = new int[size];
- System.out.println("Enter the values for the array:");
- for( int i = 0; i < size; i++ )
- data[i] = sc.nextInt();
- Arrays.sort( data );
- System.out.println("OUTPUT:");
- int pendulum_arr[] = new int[size];
- pendulum_arr = PendulumSort(data);
- for( int i = 0; i < size; i++ )
- System.out.print(pendulum_arr[i] +" ");
- System.exit(0);
- }
- public static int FetchSmallest(int[] data, int startIndex, int endIndex){
- int
- smallest = data[startIndex];
- if( endIndex >= data.length )
- endIndex = data.length - 1;
- for( int i = startIndex; i <= endIndex; i++ ){
- if( smallest > data[i] )
- smallest = data[i];
- }
- return smallest;
- }
- public static int[] PendulumSort(int data[]){
- int
- size = data.length,
- iteration = 0,
- index = ( size / 2 ),
- sorted[] = new int [ size ];
- for(int i = 0; i < size; i++)
- sorted[i] = data[i];
- boolean setLeft = true;
- while( iteration < size ){
- sorted[index] = FetchSmallest(data, iteration++, size);
- if( setLeft)
- index = index - iteration;
- else
- index = index + iteration;
- if( index < 0 || index >= size )
- break;
- setLeft = !setLeft;
- }
- return sorted;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement