Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- class pendulumArray
- {
- public static void main() {
- Scanner no = new Scanner(System.in);
- System.out.println("Enter capacity of the array");
- int capacity = no.nextInt(), array[] = new int[capacity];
- int lowest = Integer.MAX_VALUE, lowestElementIndex = capacity - 1;
- for(int x =0; x<capacity; x++) {
- System.out.print("Enter an element \t");
- int element = no.nextInt();
- array[x] = element;
- if(element<lowest) {
- lowest = element;
- lowestElementIndex = x;
- }
- }
- array[lowestElementIndex] = array[capacity/2];
- array[capacity/2] = lowest;
- int secondLowest = Integer.MAX_VALUE, secondLowestIndex = 0;
- int leftIncrement = capacity/2, rightIncrement= capacity/2;
- for(int y = 0; y< capacity -1; y++) {
- for(int z =0; z< capacity; z++) {
- if(array[z]<secondLowest && array[z]>lowest){
- secondLowest = array[z];
- secondLowestIndex = z;
- }
- }
- if(y%2==0) {
- array[secondLowestIndex] = array[--leftIncrement];
- array[leftIncrement] = secondLowest;
- }
- else {
- array[secondLowestIndex] = array[++rightIncrement];
- array[rightIncrement] = secondLowest;
- }
- lowest = secondLowest;
- secondLowest = Integer.MAX_VALUE;
- }
- for(int x =0; x<capacity; x++)
- System.out.print(array[x] + ", ");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement