You are not logged in. Please login at www.codechef.com to post your questions!

×

Please Tell me my mistake in this program

I've made this program as the demonstration of Quick Sort. I've defined it for 5 inputs but it is taking infinite. please tell me the Mistake in it.

#include<iostream>
using namespace std;
int a[5];
void QSort(int,int);
int Divide(int,int);
int main()
{
    int i;
    cout<<"Enter Elements : "<<endl;
    for(i=0;i<5;i++)
        {cin>>a[i];}
    QSort(0,4);
    cout<<"Sorted Elements are : "<<endl;
    for(i=0;i<5;i++)
        cout<<a[i];
    return 0;
}
void QSort(int first,int last)
{
        if(first<last)
        {
                int index=Divide(first,last);
                QSort(first,index-1);
                QSort(index+1,last);
        }
}
int Divide(int f,int l)
{
        int pivot=a[f];
        int left=f+1;
        int right=l;
        while(true)
        {
                while((a[left]<=pivot)&&(left<right))
                        left++;
                while((a[right]>=pivot)&&(right>left))
                        right--;
                if(left<right)
                {
                        int temp=a[left];
                        a[left]=a[right];
                        a[right]=temp;
                }
        }
        int temp=a[left];
        a[left]=a[f];
        a[f]=temp;
        return left;      
}
This question is marked "community wiki".

asked 27 Apr '12, 13:54

appy2905's gravatar image

0★appy2905
1112
accept rate: 0%

edited 27 Apr '12, 17:54

cyberax's gravatar image

3★cyberax ♦
3.4k21955

any reason for not using the qsort() function in standard library ?

(27 Apr '12, 17:56) cyberax ♦3★

because we were instructed to implement without using that function.

(07 May '12, 15:35) appy29050★

> but it is taking infinite

while(true)
{
...

of course, as your while loop never ends ! :)

link

answered 27 Apr '12, 18:10

cyberax's gravatar image

3★cyberax ♦
3.4k21955
accept rate: 20%

edited 27 Apr '12, 23:03

But while taking input, it should not me redirected to the while loop. it should be in main function only. because i am not calling any function there.

(07 May '12, 15:31) appy29050★

But you call QSort() in the main() , and in qsort function you call Divide, so it'll start an loop while(true) without any break inside, and it won's finish ;-)

(14 May '12, 17:49) vladimirmsrb2★

you have define int a[5] outside the main function.

link

answered 10 Apr '17, 15:36

karan25gupta's gravatar image

0★karan25gupta
1
accept rate: 0%

1

does that really matter? I'll show you codes where i declare array of size upto 10^6 outside main funtion which called as global declaration in programming languages

(10 Apr '17, 16:27) neilit19923★

Remove the while(true) condition with the correct condition and you're good to go ! :)

link

answered 10 Apr '17, 22:07

godslayer12's gravatar image

1★godslayer12
419210
accept rate: 7%

the program is not taking input infinite times. the program enters into a infinite loop inside the while() loop in the function Divide() because there is no terminating condition in that loop. add a terminating condition inside the while() loop so that the program an terminate.

link

answered 10 Apr '17, 22:54

karan10xb's gravatar image

2★karan10xb
11
accept rate: 0%

You have used an infinite loop...while(true) instead of true place the correct condition for loop termination

link

answered 10 Apr '17, 22:55

divyanshu_shuk's gravatar image

2★divyanshu_shuk
411
accept rate: 0%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×1,911

question asked: 27 Apr '12, 13:54

question was seen: 2,439 times

last updated: 10 Apr '17, 22:55