answersLogoWhite

0

#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

14y ago

What else can I help you with?

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 an in-place sorting algorithm?

Yes, Quick Sort is an in-place sorting algorithm.


What is the memory complexity of quick sort algorithm?

The memory complexity of the quick sort algorithm is O(log n) in the best case and O(n) in the worst case.


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 space complexity of quick sort algorithm?

The space complexity of the quick sort algorithm is O(log n) in the best and average cases, and O(n) in the worst case.


What is the space complexity of the Quick Sort algorithm?

The space complexity of the Quick Sort algorithm is O(log n) in the best and average cases, and O(n) in the worst case.


What is the time complexity of quick sort algorithm?

The time complexity of the quick sort algorithm is O(n log n) in the average case and O(n2) in the worst case.


What is the worst case time complexity of quick sort algorithm?

The worst case time complexity of the quick sort algorithm is O(n2), where n is the number of elements in the input array.


Which sorting algorithm is more efficient for large datasets: quick sort or selection sort?

Quick sort is more efficient for large datasets compared to selection sort.


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


What is the time and space complexity of the Quick Sort algorithm?

The time complexity of the Quick Sort algorithm is O(n log n) on average and O(n2) in the worst case scenario. The space complexity is O(log n) on average and O(n) in the worst case scenario.


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.