Click here to Skip to main content
15,850,917 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
public static int partition(int array[],int start,int end)
`    {
        int pivot_index=(start+end)/2;
        int pivot=array[pivot_index];
        int i=start;
        int j=end;
="" while(array[i]<="pivot)
" i++;
="" }
="" while(array[j]="">pivot)
="" swap(array,i,j);
="" }
="" return="" j;
="" }


What I have tried:

What I have tried:

I want to take pivot as the middle element. Output is not coming. Infinite loop is happening for certain test cases.For example array={3,1,2,2,2}.Please tell the modifications required in the code to get the necessary output.
Updated 2-May-23 11:12am
Patrice T 2-May-23 16:14pm    
Your code was corruoted.
Please paste again
Mike Hankey 2-May-23 18:09pm    
What do you get when you run it through the debugger?
Shuvradip Sarma 3-May-23 9:28am    
output - 3,1,2,2,2

the full code is-

import java.util.*;
class quick_sort_algo
public static void main(String args[])
Scanner sc=new Scanner(;
System.out.println("enter the length of the array");
int n=sc.nextInt();
int array[]=new int[n];
System.out.println("enter element in the array");
for(int i=0;i<array.length;i++)
="" array[i]="sc.nextInt();
" }
="" system.out.println("array="" before="" sorting");
="" for(int="" i="0;i<array.length;i++)
" system.out.print(array[i]+"="" ");
="" system.out.println();
="" int="" start="0;
" end="array.length-1;
" quick_sort_algo="" call="new" quick_sort_algo();
="" call.quick_sort(array,start,end);
="" after="" public="" static="" void="" swap(int="" array[],int="" i,int="" j)
="" t="array[i];
" array[j]="t;
" partition(int="" start,int="" end)
="" pivot_index="(start+end)/2;
" pivot="array[pivot_index];
" j="end;
" while(i<j)
="" while(array[i]<pivot)
="" i++;
="" while(="">pivot)
Richard Deeming 3-May-23 10:01am    
Still not the full code, and still corrupt.

Don't try to post code in the comments; instead, click the green "Improve question" link and update your question.
Dave Kreskowiak 2-May-23 21:24pm    
If that's all the code you have, and it was corrupted when you pasted it into your question, that's nowhere near the code required for quicksort.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900