Saturday, April 27, 2019

Peaks and Valleys


  1. Sort the array into an alternating sequence of peaks and valleys
    • Compare closed 3 items and swap them necessarily
      for (int i = 1; i < array.length; i+= 2) {
       int biggestIndex = maxIndex(array, i-1, i, i+1); 
       if (i != biggestIndex) {
         swap(array, i, biggestIndex); 
       } 
      }
  2. TODO

No comments:

Post a Comment