Member Avatar for HASHMI007
// Sorting Techniques.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>

using namespace std;
void Bubble(int x[],int n ){


int temp , j , pass;
int flag=1 ;
	for(pass=1;pass<n-1 && flag==1;pass){

	 flag=0;
		for(j=0;j<n-pass-1;j++){
		if(x[j]>x[j+1]){
		 flag =1;

		 temp=x[j];
		 x[j]=x[j+1];
		x[j+1]=temp;
		}

		}
	}
}   // Env\d off Bubble Sort funtion

int main(){
int n=5;
int a[5]={1,5,3,2,4};
for(int i=0;i<5;i++){

	Bubble(a,n);

}

}

there is problem , in main

Recommended Answers

All 4 Replies

there is problem , in main

What is problem? :icon_rolleyes: Do you take your car to the mechanic and say "Something is wrong"? Of course not, because the mechanic will find all kinds of things to fix and charge you a buttload of money for being too stupid to give a specific description of what you want fixed. It's something of an idiot tax.

Around here we won't charge you a buttload of money, but we will ignore your pleas for help. Given that you have over 50 posts, you should know how things work and ask a smart question.

Yea pretty evident...
1) Using int main with no return statement
Thats ur problem i think
2) Y r u calling it 5 times ?? Calling it once will make it sort...

3)Next thing in ur bubble sort, there is no increment of pass in the for loop.. pass++ or ++ pass.

4)Lastly, ur loop is exited after the first entrance because of && flag==1
Try it after removing that

Dont mind me, but ur program is less of a program nd more of a problem :P

// Sorting Techniques.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>

using namespace std;
void Bubble(int x[],int n ){




int main(){
int n=5;
int a[5]={1,5,3,2,4};
for(int i=0;i<5;i++){

	Bubble(a,n);

}

}

there is problem , in main

As was already mentioned, in main(), you are calling the Bubble sort function five times, and you only need to do it once. Also, you are not using a return statement at the end of your two functions main() and Bubble(). While not using a return statement won't cause any problems now, it is not "officially" correct. Main() should have a return 0; statement, and Bubble() should have simply return; . In days gone by, it was OK to omit return statements at the end of functions, but that is no longer considered correct.

So although main() doesn't have a problem that is effecting the ultimate functionality of your program, it is just unnecessarily repeating the bubble sort five times. The problem lies in your bubble sort function:

int temp , j , pass;
int flag=1 ;
	for(pass=1;pass<n-1 && flag==1;pass){

	 flag=0;
		for(j=0;j<n-pass-1;j++){
		if(x[j]>x[j+1]){
		 flag =1;

		 temp=x[j];
		 x[j]=x[j+1];
		x[j+1]=temp;
		}

		}
	}
}   // Env\d off Bubble Sort funtion
}

Lines 6 - 12 sort the array through one time, which places the contents of the array in a little better order by swapping pairs of array elements on each pass through the array. But in order to get all of the array elements all in order you have to repeat that whole swapping process quite a few number of times. But how many times? That is unknown - it all depends on how disordered the array is.

You are using another for loop as the outer loop to repeat the inner loop to finally bring all of the array elements into order: for(pass=1;pass<n-1 && flag==1;pass). But since it is unknown how many times the inner loop must run through the array and swap pairs, how are you determining that? With this ---> for(pass=1;pass<n-1 && flag==1;pass) ? I'm not sure I understand your logic there.

Here is one fact: If the array is not yet all sorted, then the if statement within the inner loop will always execute, and the only time it won't execute is when the array is all sorted, because then x[ j ] > x[ j+1 ] will always be false and thus flag will remain 0. So we can replace the outer for loop with a simpler while loop:

int flag=1 ;
	while(flag){

	 flag=0;
		for(j=0;j<n-pass-1;j++){
		if(x[j]>x[j+1]){
		 flag =1;

		 temp=x[j];
		 x[j]=x[j+1];
		x[j+1]=temp;
		}

		}
	}
}

So when flag becomes 0, because it wasn't assigned 1 by the if statement, the array is done being sorted and the sorting ends. To make the code clearer, you could write: while(flag == 1) or even while(endloop == false):

int temp, j;
bool no = TRUE;
bool yes = FALSE;
bool endloop = no;
      
	while(endloop){

	 endloop = yes;
		for(j=0;j<n-pass-1;j++){
		if(x[j]>x[j+1]){
		 endloop = no;

		 temp=x[j];
		 x[j]=x[j+1];
		x[j+1]=temp;
		}

		}
	}
}

Now only line 9 needs to be changed to make Bubble() work.

Really, the core problem here is not your coding, like in main() or the bubble sort algorithm, but the fact that you didn't understand the algorithm that you were trying to code. That is why you called Bubble() five times in main() and used this ---> for(pass=1;pass<n-1 && flag==1;pass) to end the sort instead of a simple while loop.

So my reply to this post could have just been that you don't understand the algorithm that you are trying to code and you need to go back and get to know the bubble sort algorithm, study it, and get to know it like the back of your hand. Then all of your problems would go away. That's really the issue here.

In the future, when you are having a problem with a program, ask yourself this question: Is my problem a result of my lack of understanding of what I'm trying to code, or is it my lack of understanding of the C++ language? In the case of this current post of yours, you could have asked: "I don't fully grasp how a bubble sort algorithm works. Could someone explain it to me better so that I can code it properly?

At least know where you are going wrong, instead of saying, "I think there is a problem in the main() function". What problem, a problem with your understanding of C++ in main() or a problem with understanding the problem that you are trying to code? The next time you post here with a problem, try to make that determination first!

Member Avatar for HASHMI007

thnkx i do it

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.