Quicksort 1 - Partition

Problem Statement :

```The previous challenges covered Insertion Sort, which is a simple and intuitive sorting algorithm with a running time of . In these next few challenges, we're covering a divide-and-conquer algorithm called Quicksort (also known as Partition Sort). This challenge is a modified version of the algorithm that only addresses partitioning. It is implemented as follows:

Step 1: Divide
Choose some pivot element, , and partition your unsorted array, , into three smaller arrays: , , and , where each element in , each element in , and each element in .

Example

In this challenge, the pivot will always be at , so the pivot is .

is divided into , , and .
Putting them all together, you get . There is a flexible checker that allows the elements of  and  to be in any order. For example,  is valid as well.

Given  and , partition  into , , and  using the Divide instructions above. Return a 1-dimensional array containing each element in  first, followed by each element in , followed by each element in .

Function Description

Complete the quickSort function in the editor below.

quickSort has the following parameter(s):

int arr[n]:  is the pivot element
Returns

int[n]: an array of integers as described above
Input Format

The first line contains , the size of .
The second line contains  space-separated integers  (the unsorted array). The first integer, , is the pivot element, .c```

Solution :

```                            ```Solution in C :

In   C++  :

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <cstdio>
#include <vector>
#include <cstdlib>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
using namespace std;

void partition(vector <int>  ar)
{
vector<int> glist,llist;
long long i=1, p = ar[0];
while(i<ar.size())
{
if(ar[i]>p)
glist.push_back(ar[i]);
else
llist.push_back(ar[i]);
i++;
}
i=0;
while(i<llist.size())
{
ar[i] = llist[i];
cout<<llist[i]<<" ";
i++;
}
ar[i]=p;
cout<<p<<" ";
i=0;
while(i<glist.size())
{
ar[i+p] = glist[i];
if(i==glist.size()-1)
cout<<glist[i]<<endl;
else
cout<<glist[i]<<" ";
i++;
}
}

/* Tail starts here */

int main() {
vector <int>  _ar;
int _ar_size;
cin >> _ar_size;
for(int _ar_i=0; _ar_i<_ar_size; _ar_i++) {
int _ar_tmp;
cin >> _ar_tmp;
_ar.push_back(_ar_tmp);
}

partition(_ar);

return 0;
}

In   Java  :

import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] ar = new int[n];
for(int i=0;i<n;i++){
ar[i]=in.nextInt();
}
partition(ar);
printArray(ar);
}
static void printArray(int[] ar) {
for(int n: ar){
System.out.print(n+" ");
}
System.out.println("");
}
static void partition(int[] ar) {
int p=ar[0];
int[] copy=Arrays.copyOf(ar, ar.length);
int c=0;
for(int i=1;i<ar.length;i++){
if(copy[i]<=p){
ar[c]=copy[i];
c++;
}
}
ar[c]=p;
c++;
for(int j=0;j<ar.length;j++){
if(copy[j]>p){
ar[c]=copy[j];
c++;
}
}
}
}

In   C  :

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <assert.h>

void partition(int ar_size, int *  ar) {

int f,temp=ar[0],j=0,k=0,i;
int ar1[ar_size],ar2[ar_size];
for(i=0;i<ar_size;i++)
{
if(ar[i]<temp)
{
ar1[j]=ar[i];
j++;
}
if(ar[i]>temp)
{
ar2[k]=ar[i];
k++;
//printf("k=%d\n",k);
}

}
/*  ar1[j]=temp;
j++;
for(i=0;i<k;i++)
{
ar1[j]=ar2[i];
j++;
}*/
// printf("j=%d\n",j);
for(i=0;i<j;i++)
printf("%d ",ar1[i]);
printf("%d ",temp);
for(i=0;i<k;i++)
printf("%d ",ar2[i]);

}

/* Tail starts here */
int main() {

int _ar_size;
scanf("%d", &_ar_size);
int _ar[_ar_size], _ar_i;
for(_ar_i = 0; _ar_i < _ar_size; _ar_i++) {
scanf("%d", &_ar[_ar_i]);
}

partition(_ar_size, _ar);

return 0;
}

In    Python3 :

size = int(input())
array = input().split(" ")
arr = ['None'] * size
for i in range(size):
arr[i] = int(array[i])
p = arr[0]
less = [ ]
more = [ ]
for i in range(size):
if arr[i] < p:
less.append(arr[i])
else:
more.append(arr[i])
for j in range(len(less)):
print(less[j], end = " ")
for k in range(len(more)):
print(more[k], end = " ")```
```

Queue using Two Stacks

A queue is an abstract data type that maintains the order in which elements were added to it, allowing the oldest elements to be removed from the front and new elements to be added to the rear. This is called a First-In-First-Out (FIFO) data structure because the first element added to the queue (i.e., the one that has been waiting the longest) is always the first one to be removed. A basic que

Castle on the Grid

You are given a square grid with some cells open (.) and some blocked (X). Your playing piece can move along any row or column until it reaches the edge of the grid or a blocked cell. Given a grid, a start and a goal, determine the minmum number of moves to get to the goal. Function Description Complete the minimumMoves function in the editor. minimumMoves has the following parameter(s):

Down to Zero II

You are given Q queries. Each query consists of a single number N. You can perform any of the 2 operations N on in each move: 1: If we take 2 integers a and b where , N = a * b , then we can change N = max( a, b ) 2: Decrease the value of N by 1. Determine the minimum number of moves required to reduce the value of N to 0. Input Format The first line contains the integer Q.

Truck Tour

Suppose there is a circle. There are N petrol pumps on that circle. Petrol pumps are numbered 0 to (N-1) (both inclusive). You have two pieces of information corresponding to each of the petrol pump: (1) the amount of petrol that particular petrol pump will give, and (2) the distance from that petrol pump to the next petrol pump. Initially, you have a tank of infinite capacity carrying no petr

Queries with Fixed Length

Consider an -integer sequence, . We perform a query on by using an integer, , to calculate the result of the following expression: In other words, if we let , then you need to calculate . Given and queries, return a list of answers to each query. Example The first query uses all of the subarrays of length : . The maxima of the subarrays are . The minimum of these is . The secon

QHEAP1

This question is designed to help you get a better understanding of basic heap operations. You will be given queries of types: " 1 v " - Add an element to the heap. " 2 v " - Delete the element from the heap. "3" - Print the minimum of all the elements in the heap. NOTE: It is guaranteed that the element to be deleted will be there in the heap. Also, at any instant, only distinct element