Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div>
<img src="https://i.imgur.com/OD3sZSp.png" alt="">

<h3>
PIVOT
</h3>
<li>Select last number in the array call pivot.</li>
<img src="https://i.imgur.com/XrO2FKz.png" alt="">
<li>
  Rearrange the list such that all the elements greater than of pivot are towards the right side of pivot.  All the elements lesser than the pivot will be towards the left side.  All the re-arrangements happen in the original array in-place.
</li>
<img src="https://i.imgur.com/UkvQAHQ.png" alt="">
</div>
              
            
!

CSS

              
                img {
  height:auto;
  width:100%
}

div{
  margin:20px;
}
              
            
!

JS

              
                // Space complexity is O(1) (inplace partition and sorting no extra arrayay creation)
// Time complexity is O(n log (n))
// Very optimized


function quickSort(array) {
  let startIdx = 0;
  let endIdx = array.length-1;
  performQuickSort(array, 0, endIdx);
}

function performQuickSort(array, startIdx, endIdx) {
   if (startIdx < endIdx) {
    const partitionIdx = partition(array, startIdx, endIdx);
    performQuickSort(array, startIdx, partitionIdx - 1);
    performQuickSort(array, partitionIdx + 1, endIdx);
  }
}


function partition(array, startIdx, endIdx) {
  const pivot = array[endIdx];
  let partitionIdx = startIdx;
  for (let idx = startIdx; idx < endIdx; idx++) {
    if (array[idx] <= pivot) {
      // all the elmeents lesser than pivot will be left of partitionIdx.
      swap(idx, partitionIdx, array); // Swap pivot with element at partition index. 
      partitionIdx++;
    }
  }
  swap(partitionIdx, endIdx, array);

  return partitionIdx;
}



function swap(idx1, idx2, array) {
  if(idx1===idx2) return;
  let temp = array[idx1];
  array[idx1] = array[idx2];
  array[idx2] = temp;
}


// unit tests
// do not modify the below code
describe('quickSort', function() {
  it('quicksort an arrayay', () => {
    const input = [7,6,5,4,3,2,1,0];
    const answer = quickSort(input);
    
    expect(input).toEqual([0,1,2,3,4,5,6,7]);

  });
  
   it('quicksort an arrayay', () => {
    const input = [7,2,1,6,8,5,3,4];
    const answer = quickSort(input);
    
    expect(input).toEqual([1,2,3,4,5,6,7,8]);

  });
});
              
            
!
999px

Console