<commit_before>// Problem: Quicksort                                                *
//   *                                                                    *
//   *  Prompt: Given an unsorted array of numbers,                       *
//   *          return a sorted array using Quicksort sort.               *
//   *                                                                    *
//   *  Input:  An unsorted array                                         *
//   *  Output: A sorted array                                            *
//   *                                                                    *
//   *  Example: input = [3,9,1,4,7] , output = [1,3,4,7,9]               *

// Recursively
function quicksort( arr ){ // first index technique
    //if array is empty
    if( arr.length === 0 ) return [];
    var left = [],
        right = [],
        pivot = arr[0];
    //go through each element in array except pivot
    for (var i = 1; i < arr.length; i++) {
        if( arr[i] < pivot ){
           left.push( arr[i] );
        } else {
           right.push( arr[i] );
        }
    }
    return quicksort( left ).concat( pivot, quicksort(right) );
}

// ---------------------------------------------------------

function iterativeQsort(arr) {
  var index,
      lo = 0,
      hi = arr.length-1;

  if (arr.length > 1) {
    index = partition(arr, lo, hi);
    if (l < index - 1) iterativeQsort(arr, l, index - 1);
    if (index < hi) iterativeQsort(arr, index, hi);
  }

  return arr;
}

function swap(arr, i, j) {
    var temp = arr[i];
    arr[i] = arr[j];
    arr[j] = temp;
}

function partition(arr, lo, hi){
    var pivot = arr[ Math.floor( (hi+lo)/2 )],
        i = lo,
        j = hi;

    while ( i <= j) {

      while (arr[i] < pivot) i++;
      while (arr[j] > pivot) j--;

      if (i <= j) {
          swap(arr, i, j);
          i++;
          j--;
      }
    }
    return i;
}<commit_msg>add working qSort iterative solution<commit_after>// Problem: Quicksort                                                *
//   *                                                                    *
//   *  Prompt: Given an unsorted array of numbers,                       *
//   *          return a sorted array using Quicksort sort.               *
//   *                                                                    *
//   *  Input:  An unsorted array                                         *
//   *  Output: A sorted array                                            *
//   *                                                                    *
//   *  Example: input = [3,9,1,4,7] , output = [1,3,4,7,9]               *

// Recursively
function quicksort( arr ){ // first index technique
    //if array is empty
    if( arr.length === 0 ) return [];
    var left = [],
        right = [],
        pivot = arr[0];
    //go through each element in array except pivot
    for (var i = 1; i < arr.length; i++) {
        if( arr[i] < pivot ){
           left.push( arr[i] );
        } else {
           right.push( arr[i] );
        }
    }
    return quicksort( left ).concat( pivot, quicksort(right) );
}

// ---------------------------------------------------------

function iterativeQsort(arr, lo=0, hi=arr.length-1) {
  var index;

  if (arr.length > 1) {
    index = partition(arr, lo, hi);
    if (lo < index - 1) iterativeQsort(arr, lo, index - 1);
    if (index < hi) iterativeQsort(arr, index, hi);
  }

  return arr;
}

function swap(arr, i, j) {
    var temp = arr[i];
    arr[i] = arr[j];
    arr[j] = temp;
}

function partition(arr, lo, hi){
    var pivot = arr[ Math.floor( (hi+lo)/2 )],
        i = lo,
        j = hi;

    while ( i <= j) {

      while (arr[i] < pivot) i++;
      while (arr[j] > pivot) j--;

      if (i <= j) {
          swap(arr, i, j);
          i++;
          j--;
      }
    }
    return i;
}
{'fraction_non_alphanumeric': 0.16326530612244897, 'fraction_numerical': 0.009816584861792819, 'mean_word_length': 0.9825908858166923, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4717829', 'n_tokens_mistral': 1132, 'n_tokens_neox': 1090, 'n_words': 312}