211k views
1 vote
Write a program, using any language you want, to sort the following using QuickSort: {3, 4, 5,1, 2, 6, 7, 8}

1 Answer

5 votes

Answer:

The code below was developed in C language. Please scroll down for the output results.

Step-by-step explanation:

#include <stdio.h>

//define the quicksort

void quicksort (int[], int, int);

int

main ()

{

int ls[50]; //list

int sz, i; //size

//input elements

printf ("input the quantity of elements: ");

scanf ("%d", &sz);

printf ("input the elements:\\");

for (i = 0; i < sz; i++)

{

scanf ("%d", &ls[i]);

}

quicksort (ls, 0, sz - 1);

printf ("output quick sort\\");

for (i = 0; i < sz; i++)

{

printf ("%d ", ls[i]);

}

printf ("\\");

return 0;

}

void

quicksort (int ls[], int low, int high)

{

int pv, i, j, temp;

if (low < high)

{

pv = low;

i = low;

j = high;

while (i < j)

{

while (ls[i] <= ls[pv] && i <= high)

{

i++;

}

while (ls[j] > ls[pv] && j >= low)

{

j--;

}

if (i < j)

{

temp = ls[i];

ls[i] = ls[j];

ls[j] = temp;

}

}

temp = ls[j];

ls[j] = ls[pv];

ls[pv] = temp;

quicksort (ls, low, j - 1);

quicksort (ls, j + 1, high);

}

}

Output console

input the quantity of elements:

> 4

input the elements:

> 123

> 45

> 1

> 5

output quick sort

1 5 45 123

User Kjv
by
6.8k points