Bubble Sort in C, help to find bug - Techist - Tech Forum

Go Back   Techist - Tech Forum > Computer Software > Programming
Click Here to Login
Reply
 
Thread Tools Display Modes
 
Old 07-07-2012, 07:09 AM   #1 (permalink)
Newb Techie
 
Join Date: Jul 2012
Location: India
Posts: 11
Unhappy Bubble Sort in C, help to find bug

I have created this program.

[C] #include<stdio.h> void main() { int i,j,temp,no,type,a[10]; printf(" - Pastebin.com

surprisingly I found that some times it works, some times not. Using Code::Blocks GCC as compiler

Case of Failure




Successful Case

I can't figure out whats wrong here, help please
__________________

reLoadreFresh is offline   Reply With Quote
Old 07-07-2012, 08:42 AM   #2 (permalink)
Knowitall!
 
strollin's Avatar
 
Join Date: May 2011
Location: N. Calif.
Posts: 622
Default Re: Bubble Sort in C, help to find bug

Just off the top of my head and based on your fail/success cases, it appears your program can't handle ints with more than 3 digits. Does it ever fail with smaller numbers?

A couple of suggestions regarding your code, why do you check the sort type (ascend/descend) for every sort step? You should be able to write your code where you only need to do that check once. Instead of prompting the user for how many numbers will be entered, why don't you write it so that the user enters as many numbers as they need the program could keep looping until the user enters a null (presses enter without typing in a number).
__________________

strollin is offline   Reply With Quote
Old 07-07-2012, 01:23 PM   #3 (permalink)
Newb Techie
 
Join Date: Jul 2012
Location: India
Posts: 11
Default Re: Bubble Sort in C, help to find bug

Quote:
Originally Posted by strollin View Post
Just off the top of my head and based on your fail/success cases, it appears your program can't handle ints with more than 3 digits. Does it ever fail with smaller numbers?

A couple of suggestions regarding your code, why do you check the sort type (ascend/descend) for every sort step? You should be able to write your code where you only need to do that check once. Instead of prompting the user for how many numbers will be entered, why don't you write it so that the user enters as many numbers as they need the program could keep looping until the user enters a null (presses enter without typing in a number).
It working fine with all 4 digit no. , all 3,2,1 digit no. (not tried 5, bcs of int limit), I still couldn't figure out in which cases, its giving wrong output. :/
thnks for ur suggestion, will move order check condition, right before first loop. any way thanks for reply
reLoadreFresh is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Air Bubble blasterfreak88 Cooling 0 03-21-2010 10:45 PM
Project Bubble Osiris Tips, Tricks & Tutorials 0 02-14-2010 03:21 PM
Speech Bubble Osiris Tips, Tricks & Tutorials 0 04-17-2009 09:45 AM
Desktop Apps with Bubble Osiris Tips, Tricks & Tutorials 0 07-18-2008 07:06 AM
Has the PC bubble burst? beedubaya Off Topic Discussion 14 04-21-2008 06:47 PM



Copyright 2002- Social Knowledge, LLC All Rights Reserved.

All times are GMT -5. The time now is 12:15 PM.


Powered by vBulletin® Version 3.8.8 Beta 1
Copyright ©2000 - 2017, vBulletin Solutions, Inc.