Answers for "quick sort search"

C
1

quick sort algorithm

def partition(a,l,h):
    pivot = a[l]
    i = l
    j=h
    while i<j:
        while a[i]<=pivot and i<h: i+=1
        while a[j]>pivot and j>l: j-=1
        if i<j: a[i],a[j]=a[j],a[i]
        
    a[j],a[l]=a[l],a[j]
    return j

def quickSort(a,l,h):
    if l < h:
        pi = partition(a, l, h)
        quickSort(a, l, pi - 1)
        quickSort(a, pi + 1, h)
        
#driver Code        
a =[10, 7, 8, 9, 1, 5 ]
quickSort(a, 0, len(a) - 1)
print(a)
#Output: [1, 5, 7, 8, 9, 10]
Posted by: Guest on September-11-2021
1

quick sort algorithm

def partition(a,l,h):
    pivot = a[l]
    i = l
    j=h
    while i<j:
        while a[i]<=pivot and i<h: i+=1
        while a[j]>pivot and j>l: j-=1
        if i<j: a[i],a[j]=a[j],a[i]
        
    a[j],a[l]=a[l],a[j]
    return j

def quickSort(a,l,h):
    if l < h:
        pi = partition(a, l, h)
        quickSort(a, l, pi - 1)
        quickSort(a, pi + 1, h)
        
#driver Code        
a =[10, 7, 8, 9, 1, 5 ]
quickSort(a, 0, len(a) - 1)
print(a)
#Output: [1, 5, 7, 8, 9, 10]
Posted by: Guest on September-11-2021
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
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

Code answers related to "C"

Browse Popular Code Answers by Language