Answers for "quick sort in place"

3

quicksort

// @see https://www.youtube.com/watch?v=es2T6KY45cA&vl=en
// @see https://www.youtube.com/watch?v=aXXWXz5rF64
// @see https://www.cs.usfca.edu/~galles/visualization/ComparisonSort.html

function partition(list, start, end) {
    const pivot = list[end];
    let i = start;
    for (let j = start; j < end; j += 1) {
        if (list[j] <= pivot) {
            [list[j], list[i]] = [list[i], list[j]];
            i++;
        }
    }
    [list[i], list[end]] = [list[end], list[i]];
    return i;
}

function quicksort(list, start = 0, end = undefined) {
    if (end === undefined) {
        end = list.length - 1;
    }
    if (start < end) {
        const p = partition(list, start, end);
        quicksort(list, start, p - 1);
        quicksort(list, p + 1, end);
    }
    return list;
}

quicksort([5, 4, 2, 6, 10, 8, 7, 1, 0]);
Posted by: Guest on May-31-2020
0

quick sort

#include<stdio.h>
int partition(int arr[], int low, int high) {
  int temp;
  int pivot = arr[high];
  int i = (low - 1); 
  for (int j = low; j <= high - 1; j++) {
    if (arr[j] <= pivot) { 
      i++; 
      temp = arr[i];
      arr[i] = arr[j];
      arr[j] = temp;
    } 
  } 
  temp = arr[i + 1];
  arr[i + 1] = arr[high];
  arr[high] = temp;
  return (i + 1); 
} 
void quick_sort(int arr[], int low, int high) { 
  if (low < high) {
    int pi = partition(arr, low, high); 
    quick_sort(arr, low, pi - 1); 
    quick_sort(arr, pi + 1, high); 
  } 
} 
int print(int arr[], int n) {
  for(int i = 0; i < n; i++) {
    printf("%d ", arr[i]);
  }
}

int main()
{
int n, i;
scanf("%d", &n);
int arr[n];
for(i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
quick_sort(arr, 0, n - 1);
print(arr, n);
}
Posted by: Guest on April-17-2021
1

quick sort

//I Love Java
import java.io.*;
import java.util.*;
import java.util.stream.*;
import static java.util.Collections.*;

import static java.util.stream.Collectors.*;

public class Quick_Sort_P {

    static void swap(List<Integer> arr, int i, int j) {
        int temp = arr.get(i);
        arr.set(i, arr.get(j));
        arr.set(j, temp);
    }

    static int partition(List<Integer> arr, int low, int high) {

        int pivot = arr.get(high);
        int i = (low - 1);

        for (int j = low; j <= high - 1; j++) {

            if (arr.get(j) < pivot) {

                i++;
                swap(arr, i, j);
            }
        }
        swap(arr, i + 1, high);
        return (i + 1);
    }

    static void quickSort(List<Integer> arr, int low, int high) {
        if (low < high) {

            int pi = partition(arr, low, high);

            quickSort(arr, low, pi - 1);
            quickSort(arr, pi + 1, high);
        }
    }

    public static void main(String[] args) throws IOException {

        BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));

        List<Integer> arr = Stream.of(buffer.readLine().replaceAll("\\s+$", "").split(" ")).map(Integer::parseInt)
                .collect(toList());

        int n = arr.size();

        quickSort(arr, 0, n - 1);
        System.out.println("Sorted array: ");
        System.out.println(arr);
    }
}
Posted by: Guest on June-11-2021

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language