0% found this document useful (0 votes)
22 views2 pages

Input:: Write A Program To Perform Quick Sort On A Set of Entries From A File Using Recursion

This program implements quicksort recursion to sort integers read from a file. It prompts the user for the number of elements, reads the elements into an array, calls the quicksort function with the array and indices, and prints the sorted array. The quicksort function takes the array, starting index, and ending index, chooses a pivot element, partitions the array around the pivot, and recursively calls itself on the subarrays.

Uploaded by

Ritesh Vashisht
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views2 pages

Input:: Write A Program To Perform Quick Sort On A Set of Entries From A File Using Recursion

This program implements quicksort recursion to sort integers read from a file. It prompts the user for the number of elements, reads the elements into an array, calls the quicksort function with the array and indices, and prints the sorted array. The quicksort function takes the array, starting index, and ending index, chooses a pivot element, partitions the array around the pivot, and recursively calls itself on the subarrays.

Uploaded by

Ritesh Vashisht
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Question 4

Write a program to Perform Quick Sort on a set of Entries from a File using Recursion.

INPUT:

#include <stdio.h>
 
void quicksort (int [], int, int);
 
int main()
{
  int list[50];
  int size, i;
 
  printf("Enter the number of elements: ");
  scanf("%d", &size); 
  printf("Enter the elements to be sorted:\n");
  for (i = 0; i < size; i++)
  {
    scanf("%d", &list[i]);
  } 
  quicksort(list, 0, size - 1);
  printf("After applying quick sort\n");
  for (i = 0; i < size; i++)
  {
    printf("%d ", list[i]);
  }
  printf("\n");
 
  return 0;
}
void quicksort(int list[], int low, int high)
{
  int pivot, i, j, temp;
  if (low < high)
  {
    pivot = low;
    i = low;
    j = high;
    while (i < j) 
    {
      while (list[i] <= list[pivot] && i <= high)
      {
        i++;
      }
      while (list[j] > list[pivot] && j >= low)
      {
        j--;
      }
      if (i < j)
      {
        temp = list[i];
        list[i] = list[j];
        list[j] = temp;
      }
    }
    temp = list[j];
    list[j] = list[pivot];
    list[pivot] = temp;
    quicksort(list, low, j - 1);
    quicksort(list, j + 1, high);
  }
}

You might also like