Advertisement
lordvader31

pendulumArray

Jun 20th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.60 KB | None | 0 0
  1. import java.util.Scanner;
  2. class pendulumArray
  3. {
  4.     public static void main() {
  5.         Scanner no = new Scanner(System.in);
  6.         System.out.println("Enter capacity of the array");
  7.         int capacity = no.nextInt(), array[] = new int[capacity];
  8.         int lowest = Integer.MAX_VALUE, lowestElementIndex = capacity - 1;
  9.         for(int x =0; x<capacity; x++) {
  10.             System.out.print("Enter an element \t");
  11.             int element = no.nextInt();
  12.             array[x] = element;
  13.             if(element<lowest) {
  14.                 lowest = element;
  15.                 lowestElementIndex = x;
  16.             }
  17.         }
  18.         array[lowestElementIndex] = array[capacity/2];
  19.         array[capacity/2] = lowest;
  20.         int secondLowest = Integer.MAX_VALUE, secondLowestIndex = 0;
  21.         int leftIncrement = capacity/2, rightIncrement= capacity/2;
  22.         for(int y = 0; y< capacity -1; y++) {
  23.             for(int z  =0; z< capacity; z++) {
  24.                 if(array[z]<secondLowest && array[z]>lowest){
  25.                     secondLowest = array[z];
  26.                     secondLowestIndex = z;
  27.                 }
  28.             }  
  29.             if(y%2==0) {
  30.                 array[secondLowestIndex] = array[--leftIncrement];
  31.                 array[leftIncrement] = secondLowest;
  32.             }
  33.             else {
  34.                 array[secondLowestIndex] = array[++rightIncrement];
  35.                 array[rightIncrement] = secondLowest;
  36.             }
  37.             lowest = secondLowest;
  38.             secondLowest = Integer.MAX_VALUE;
  39.         }
  40.         for(int x =0; x<capacity; x++)
  41.             System.out.print(array[x] + ", ");
  42.    }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement