answersLogoWhite

0

Write an algorithm for quick sort?

Updated: 8/11/2023
User Avatar

Wiki User

9y ago

Best Answer

#include <stdio.h>

#include <stdlib.h>

#define size 50

void swap(int *x,int *y)

{

int temp;

temp = *x;

*x = *y;

*y = temp;

}

int partition(int i,int j )

{

return((i+j) /2);

}

void quicksort(int list[],int m,int n)

{

int key,i,j,k;

if( m < n)

{

k = partition(m,n);

swap(&list[m],&list[k]);

key = list[m];

i = m+1;

j = n;

while(i <= j)

{

while((i <= n) && (list[i] <= key))

i++;

while((j >= m) && (list[j] > key))

j--;

if( i < j)

swap(&list[i],&list[j]);

}

// swap two elements

swap(&list[m],&list[j]);

// recursively sort the lesser list

quicksort(list,m,j-1);

quicksort(list,j+1,n);

}

}

void printlist(int list[],int n)

{

int i;

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

printf("%d\t",list[i]);

}

void main()

{

int n,i;

int list[size];

printf("How many numbers do you want to enter");

scanf("%d",&n);

printf("Enter the numbers you want to sort");

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

{

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

}

printf("The list before sorting is:\n");

printlist(list,n);

// sort the list using quicksort

quicksort(list,0,n-1);

// print the result

printf("The list after sorting using quicksort algorithm:\n");

printlist(list,n);

}

User Avatar

Wiki User

13y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

15y ago

//Quick Sort Functions for Descending Order

// (2 Functions)

void quicksort(apvector &array, int top, int bottom)

{

// top = subscript of beginning of vector being considered

// bottom = subscript of end of vector being considered

// this process uses recursion - the process of calling itself

int middle;

if (top < bottom)

{

middle = partition(array, top, bottom);

quicksort(array, top, middle); // sort top partition

quicksort(array, middle+1, bottom); // sort bottom partition

}

return;

}

//Function to determine the partitions

// partitions the array and returns the middle index (subscript)

int partition(apvector &array, int top, int bottom)

{

int x = array[top];

int i = top - 1;

int j = bottom + 1;

int temp;

do

{

do

{

j - -;

}while (x >array[j]);

do

{

i++;

} while (x

if (i < j)

{

temp = array[i]; // switch elements at positions i and j

array[i] = array[j];

array[j] = temp;

}

}while (i < j);

return j; // returns middle index

}

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

If you mean sort the individual characters in a String:

{

String str; // the string to sort

char[] charArray = str.toCharArray();

java.util.Arrays.sort(charArray);

str = new String(charArray);

}

If you mean sort an array of Strings:

{

String[] strs; // the array of string to sort

java.util.Arrays.sort(strs);

} bubble sort technique. public class bubbleSort { public static void main(String a[]) { int i; int array[] = {12,9,4,99,120,1,3,10}; System.out.println("Values Before the sort:\n"); for(i = 0; i < array.length; i++) System.out.print( array[i]+" "); System.out.println(); bubble_srt(array, array.length); System.out.print("Values after the sort:\n"); for(i = 0; i a[j]){ t = a[j-1]; a[j-1]=a[j]; a[j]=t; } } } } }

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

Given an array, A, and the lower and upper bounds of a subarray within the array, sort the subarray:

1. If lower bound is not less than upper bound then exit. The subarray is sorted.

2. Pick a pivot value from the subarray at random

3. Swap the pivot value with the value located in the upper bound

4. Let left = lower bound

5. Let right = upper bound

6. Repeat while left<right

{

6.1. Increment left while left value is less than pivot value

6.2 Decrement right while right value is not less than pivot value

6.3. If left<right, swap left value with right value

}

7. Swap left value with upper bound value (the pivot)

8. Repeat algorithm using the current lower bound and left-1 as the upper bound

9. Repeat algorithm using the current upper bound and left+1 as the lower bound

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write an algorithm for quick sort?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is the benefit of randomization in quick sort algorithm?

You don't waste time computing a pivot.


What fundamental mathematical principle underlies the effectiveness of the Quick sort algorithm to sort certain lists of data elements rapidly?

penis


How do you sort an array of numbers?

Use a sorting algorithm. There are a bewildering number of sorting algorithms, both stable and unstable. To sort numbers, an unstable sort suffices. The algorithm you use will depend on how many numbers need to be sorted (a small or a large set), however a hybrid algorithm (a combination of two or more algorithms) can cater for both. Introsort (unstable) and timsort (stable) are the two most common hybrid sorting algorithms.


When would you use bubble sort?

Never. Bubble sort is often cited as an example of how not to write a sorting algorithm and is used purely as a programming exercise. It is never used in production code. Although reasonably efficient when sorting small lists, an insertion sort performs better on average. But for larger lists it has no practical uses. A merge sort is better for large lists, but if stability isn't an issue a quick sort is even better. Hybrid sorts typically use quick sort until a partition is small enough for an insertion sort to complete the job.


What is algorithm to write algorithm to the program to access a pointer variable in structure?

Here is the algorithm of the algorithm to write an algorithm to access a pointer in a variable. Algorithmically.name_of_the_structure dot name_of_the _field,eg:mystruct.pointerfield

Related questions

Is quick sort is an example of dynamic programming algorithm?

quick sort is a divide and conquer method , it is not dynamic programming


What is the benefit of randomization in quick sort algorithm?

You don't waste time computing a pivot.


What fundamental mathematical principle underlies the effectiveness of the Quick sort algorithm to sort certain lists of data elements rapidly?

penis


How do you sort an array of numbers?

Use a sorting algorithm. There are a bewildering number of sorting algorithms, both stable and unstable. To sort numbers, an unstable sort suffices. The algorithm you use will depend on how many numbers need to be sorted (a small or a large set), however a hybrid algorithm (a combination of two or more algorithms) can cater for both. Introsort (unstable) and timsort (stable) are the two most common hybrid sorting algorithms.


What are the advantages for bubble sort?

Bubble sort has no practical applications other than that it is often cited as an example of how not to write an algorithm. Insert sort is the best algorithm for sorting small lists of items and is often used in conjunction with quick sort to sort larger lists. Like insert sort, bubble sort is simple to implement and is a stable sort (equal items remain in the same order they were input). However, insert sort uses copy or move operations rather than swaps (which is actually three operations per swap) and is therefore quicker. The only time a bubble sort will work quicker than insert sort is when the array is already sorted, which renders the entire algorithm redundant. A modified algorithm that specifically tests if an array is sorted or not would be more efficient than a single-pass bubble sort.


When would you use bubble sort?

Never. Bubble sort is often cited as an example of how not to write a sorting algorithm and is used purely as a programming exercise. It is never used in production code. Although reasonably efficient when sorting small lists, an insertion sort performs better on average. But for larger lists it has no practical uses. A merge sort is better for large lists, but if stability isn't an issue a quick sort is even better. Hybrid sorts typically use quick sort until a partition is small enough for an insertion sort to complete the job.


What is algorithm to write algorithm to the program to access a pointer variable in structure?

Here is the algorithm of the algorithm to write an algorithm to access a pointer in a variable. Algorithmically.name_of_the_structure dot name_of_the _field,eg:mystruct.pointerfield


Write an algorithm to find the root of quadratic equation?

Write an algorithm to find the root of quadratic equation


In a sorting algorithm the sort order can be changed by changing the operator?

In a sorting algorithm the sort order can be changed by changing the comparison operator.


A Write the algorithm to concatenate two given strings?

a write the algorithm to concatenate two given string


Explain and illustrate insertion sort algorithm to short a list of n numburs?

Explain and illustrate insertion sort algorithm to short a list of n numburs


If quick sort and merge sort have same elements then what will be the complexity of both algorithms?

quicksort should be O(n^2), but merge sort should be O(nlogn). but if you can modify partition algorithm with checking all values same in array from p to r, it could be O(nlogn).