Question

In: Computer Science

Please use C language to code all of the problems below. Please submit a .c file...

Please use C language to code all of the problems below. Please submit a .c file for each of the solutions, that includes the required functions, tests you wrote to check your code and a main function to run the code.

Q2. Implement the quick-sort algorithm.

Solutions

Expert Solution

C CODE :

#include<stdio.h>

int partition (int a[], int down, int up)
{
   int pivot = a[up]; // pivot element
   int i = (down - 1);

   for (int j = down; j <= up- 1; j++)
   {
       if (a[j] < pivot)
       {
           i++;
           int temp = a[i];
           a[i] = a[j];
           a[j] = temp;
       }
   }
   int temp1 = a[i + 1];
   a[i + 1] = a[up];
   a[up] = temp1;
   return (i + 1);
}

void sort(int a[], int down, int up)
{
   if (down < up)
   {
       int pivot = partition(a, down, up); // call partition
       sort(a, down, pivot - 1); // recursive function for left hand of array
       sort(a, pivot + 1, up); // recursive function for right hand of array
   }
}

int main()
{
int n;
printf("Enter the size of the array : ");
scanf("%d", &n); // read the array size
   int a[n];
   printf("Enter the array elements : ");
   for(int i = 0; i < n; i++)
   {
   scanf("%d", &a[i]); // read the array elements
   }
  
   sort(a, 0, n - 1); // call the sort function
   printf("After sorting the array : ");
   for (int i = 0; i < n; i++)
   {
       printf("%d ", a[i]); // print the array after sorting
   }
   return 0;
}

OUTPUT :


Related Solutions

Create a CodeBlocks project with a main.cpp file. Submit the main.cpp file in Canvas. C++ Language...
Create a CodeBlocks project with a main.cpp file. Submit the main.cpp file in Canvas. C++ Language A Game store sells many types of gaming consoles. The console brands are Xbox, Nintendo, PlayStation. A console can have either 16 or 8 gigabytes of memory. Use can choose the shipping method as either Regular (Cost it $5) or Expedite (Cost is $10) The price list is given as follows: Memory size/Brand Xbox Nintendo PlayStation 16 gigabytes 499.99 469.99 409.99 8 gigabytes 419.99...
Instructions: 1. Please use only C as the language of programming. 2. Please submit the following:...
Instructions: 1. Please use only C as the language of programming. 2. Please submit the following: (1) the client and the server source files each (2) a brief Readme le that shows the usage of the program. 3. Please appropriately comment your program and name all the identifiers suitable, to enable enhanced readability of the code. Problem: Write an ftp client and an ftp server such that the client sends a request to ftp server for downloading a file. The...
Please code by C++ The required week2.cpp file code is below Please ask if you have...
Please code by C++ The required week2.cpp file code is below Please ask if you have any questions instruction: 1. Implement the assignment operator: operator=(Vehicle &) This should make the numWheels and numDoors equal to those of the object that are being passed in. It is similar to a copy constructor, only now you are not initializing memory. Don’t forget that the return type of the function should be Vehicle& so that you can write something like: veh1 = veh2...
please submit the C code( no third party library). the C code will create output to...
please submit the C code( no third party library). the C code will create output to a file, and iterate in a loop 60 times and each iteration is 1 second, and if any key from your keyboard is pressed will write a 1 in the file, for every second no key is pressed, will write a 0 into the output file.
Please provide solutions to the following problems. Please use Excel to solve the problems and submit...
Please provide solutions to the following problems. Please use Excel to solve the problems and submit the Excel spreadsheet. A fair coin is tossed 15 times, calculate the probability of getting 0 heads or 15 heads A biased coin with probability of head being .6 is tossed 12 times. What is the probability that number of head would more than 4 but less than or equal to 10. You have a biased dice (with six faces numbered 1,2,3,4,5 and 6)...
Please provide solutions to the following problems. Please use Excel to solve the problems and submit...
Please provide solutions to the following problems. Please use Excel to solve the problems and submit the Excel spreadsheet. You started a new restaurant. Based on invoices for the first 30 days, you estimated your average grocery bill to be $20,000 with a standard deviation of $2000. You want to start another restaurant in a similar neighborhood and you are planning to prepare a brochure for investors and to work out a deal with a whole sale food distributor. Prepare...
Please complete in MASM (x86 assembly language). Use the code below to get started. Use a...
Please complete in MASM (x86 assembly language). Use the code below to get started. Use a loop with indirect or indexed addressing to reverse the elements of an integer array in place. Do not copy the elements to any other array. Use the SIZEOF, TYPE, and LENGTHOF operators to make the program as flexible as possible if the array size and type should be changed in the future. .386 .model flat,stdcall .stack 4096 ExitProcess PROTO,dwExitCode:DWORD .data    ; define your...
Please answer the problem below in C programming language: Create a pointer activity source file -...
Please answer the problem below in C programming language: Create a pointer activity source file - cptr2.c - that takes two arguments, a number from 1 to 3, and a string sentence(s). Create variables for a character, an integer, a string pointer. Based on integer value you will use that number of string pointers. The string variable is a string pointer that has not been allocated.    Define pointers to those variables types without any initialization of those points to the...
Use Python to Complete the following on a single text file and submit your code and...
Use Python to Complete the following on a single text file and submit your code and your output as separate documents. For each problem create the necessary list objects and write code to perform the following examples: Sum all the items in a list. Multiply all the items in a list. Get the largest number from a list. Get the smallest number from a list. Remove duplicates from a list. Check a list is empty or not. Clone or copy...
please use text only! Instructions Consider the provided C++ code in the main.cpp file: The function...
please use text only! Instructions Consider the provided C++ code in the main.cpp file: The function func2 has three parameters of type int, int, and double, say a, b, and c, respectively. Write the definition of func2 so that its action is as follows: Prompt the user to input two integers and store the numbers in a and b, respectively. If both of the numbers are nonzero: If a >= b, the value assigned to c is a to the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT