Question

In: Computer Science

Write a program, using any language you want, and any sorting algorithm discussed in this unit...

Write a program, using any language you want, and any sorting algorithm discussed in this unit to sort the following :

243, 1, 4, 6, 234, 33, 674, 32, 3333

Note: Can you write it in quick sort

Solutions

Expert Solution

Doing the quick sort in C language :

#include<stdio.h>

void swap(int* a, int* b)
{
int tmp = *a;
*a = *b;
*b = tmp;
}


int partition (int a[], int l, int h)
{
int pivot = a[h]; // pivot
int ind = (l - 1); // Index of smaller element

for (int j = l; j <= h- 1; j++)
{
// If current element is smaller than the pivot
if (a[j] < pivot)
{
ind++; // increment index of smaller element
swap(&a[ind], &a[j]);
}
}
swap(&a[ind + 1], &a[h]);
return (ind + 1);
}

void quickSort(int a[], int l, int h)
{
if (l < h)
{
// p is the partitioning index, a[p] is now at right place
int p = partition(a, l, h);

quickSort(a, l, p - 1); //sort the elements before the partition
quickSort(a, p + 1, h); ////sort the elements after the partition
}
}

void printArr(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}


int main()
{
int a[] = {243, 1, 4, 6, 234, 33, 674, 32, 3333};
int num = sizeof(a)/sizeof(a[0]);
quickSort(a, 0, num-1);
printf("The Sorted array is : \n");
printArr(a, num);
return 0;
}

Here is the screenshot of the code and it's output:


Related Solutions

Write a program, using any language you want, that uses a Randomized-Select algorithm. Briefly explain what...
Write a program, using any language you want, that uses a Randomized-Select algorithm. Briefly explain what it does, how it does. Include code and show output.
Write a program, using any language you want, to sort the following using QuickSort: {3, 4,...
Write a program, using any language you want, to sort the following using QuickSort: {3, 4, 5,1, 2, 6, 7, 8}
Write a program to sort the following using HeapSort. Use any language you want. A=[10, 8,...
Write a program to sort the following using HeapSort. Use any language you want. A=[10, 8, 7, 9,16, 14, 3, 2, 4, 1]
Write a program using C language that -ask the user to enter their name or any...
Write a program using C language that -ask the user to enter their name or any other string (must be able to handle multiple word strings) - capture the epoch time in seconds and the corresponding nanoseconds - ask the user to type in again what they entered previously - capture the epoch time in seconds and the corresponding nanoseconds -perform the appropriate mathematical calculations to see how long it took in seconds and nanoseconds (should show to 9 decimal...
Write a program that performs a merge-sort algorithm without using a recursion. c++ programming language(Only #inlclude...
Write a program that performs a merge-sort algorithm without using a recursion. c++ programming language(Only #inlclude <iostream>)
Programming Language: JAVA In this assignment you will be sorting an array of numbers using the...
Programming Language: JAVA In this assignment you will be sorting an array of numbers using the bubble sort algorithm. You must be able to sort both integers and doubles, and to do this you must overload a method. Bubble sort work by repeatedly going over the array, and when 2 numbers are found to be out of order, you swap those two numbers. This can be done by looping until there are no more swaps being made, or using a...
Write a program in C language that uses a binary search algorithm to guess a number...
Write a program in C language that uses a binary search algorithm to guess a number from 1 to 100. The computer will keep guessing until they get the users number correct.
Develop an algorithm and implement a Preemptive Priority scheduling algorithm using C++ and using bubble sorting...
Develop an algorithm and implement a Preemptive Priority scheduling algorithm using C++ and using bubble sorting .Arrival time, burst time and priority values.The code should also display: (i) Gantt chart and determine the following: (ii) Determine the Turnaround time(TAT), waiting time(WT) of each process (iii) Determine the Average Waiting Time (AWT) and Average Turnaround Time (ATAT) of all processes. please write the comments
Write an MSP430 assembly language program that implements the following algorithm: a macro called "vdot" that...
Write an MSP430 assembly language program that implements the following algorithm: a macro called "vdot" that calculates the "dot product" of two vectors "a" and "b", implemented as “arrays” (following the “C” language convention), of 3 elements. the macro should receive 2 pointers to the first element of each vector and return the result in R13. I have another file where I save my vectors called, "vars.c" here I save: int a[] = { 14, 65, 9} int b[] =...
Write an MSP430 assembly language program that implements the following algorithm: a subroutine, called 'numadd' that...
Write an MSP430 assembly language program that implements the following algorithm: a subroutine, called 'numadd' that sums up all the numeric characters present in a phrase ("string" that follows the "C" language convention). By For example, if the phrase is "Today is the 28th of month 9", the subroutine must perform the following sum: 2 + 8 + 9 = 19. The subroutine must receive the address of the first character of the corresponding phrase in the "stack", and return...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT