School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,494 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,870 people online right now. Registration is fast and FREE... Join Now!




bubble sort logic

 

bubble sort logic

iproffitt

23 Apr, 2009 - 12:51 PM
Post #1

New D.I.C Head
*

Joined: 23 Apr, 2009
Posts: 1

hello, could someone help me understand bubble sort logic, I'm a beginning programmer. Also I would like to understand some pseudocode: The city of Cary is holding a special census. The census takers collect one record for each resident. Each record contains a resident’s age, gender, marital status, and voting district. The voting district field contains a number from 1 to 22. Design a program that accepts data fro each resident until all have been entered and then produce a list of all 22 districts and the number of residents in each.
Thank you

User is offlineProfile CardPM
+Quote Post


Core

RE: Bubble Sort Logic

23 Apr, 2009 - 04:27 PM
Post #2

Den The Developer
Group Icon

Joined: 8 Dec, 2008
Posts: 2,944



Thanked: 214 times
Dream Kudos: 900
Expert In: C#, VB.NET, .NET Framework

My Contributions
Are you receiving any errors? Does this code not work that way you intended it? When asking for help there are a couple items that are vital in order for someone to properly help you:
  • Post the code you're having problems with
  • Post the exact error you're receiving, if you are receiving one
  • If no error explain what the code is doing versus what you want it to do
  • Post your question in the body of your post, not the description field

User is offlineProfile CardPM
+Quote Post

metalgtr84

RE: Bubble Sort Logic

15 May, 2009 - 03:51 PM
Post #3

New D.I.C Head
*

Joined: 14 May, 2009
Posts: 13

Bubblesort is the easiest to learn sorting algorithm, but also the most inefficient. The basic idea is that you have a list of things to sort, and you compare each item with the item next to it. The bigger (or smaller) items "bubble" to the top of the list and your list eventually becomes sorted. The reason this is so inefficient is that, for n items, you have to do this n^2 times before the list is sorted - usually with a FOR loop inside of a FOR loop.
User is offlineProfile CardPM
+Quote Post

masteryee

RE: Bubble Sort Logic

19 May, 2009 - 05:52 PM
Post #4

D.I.C Regular
***

Joined: 16 May, 2009
Posts: 266



Thanked: 36 times
My Contributions
As for your pseudo code, it sounds like you need to design a program that continually prompts the user to create new residents until you want to print out the district summary. Every time your user agrees to create a resident, the user is prompted to enter the resident's age, gender, marital status, and voting district. Your program will store this information somewhere in an array or list. When the user is done creating residents, your program should at some point in time offer the user an option to view the district summary. When the user selects that option, your program will print a list of the districts and their corresponding count of its residents.

There are of course many other ways to design this program (i.e. you can create a file with resident data and read the data into your program instead).
User is offlineProfile CardPM
+Quote Post

Jubb

RE: Bubble Sort Logic

20 May, 2009 - 01:19 PM
Post #5

D.I.C Head
**

Joined: 6 May, 2009
Posts: 76



Thanked: 5 times
My Contributions
QUOTE(metalgtr84 @ 15 May, 2009 - 03:51 PM) *

Bubblesort is the easiest to learn sorting algorithm, but also the most inefficient. The basic idea is that you have a list of things to sort, and you compare each item with the item next to it. The bigger (or smaller) items "bubble" to the top of the list and your list eventually becomes sorted. The reason this is so inefficient is that, for n items, you have to do this n^2 times before the list is sorted - usually with a FOR loop inside of a FOR loop.


It's only inefficient if you actually have to sort the data! Or if you have a large amount of data. Other than that we're looking at a best case complexity of O(n) !!
User is offlineProfile CardPM
+Quote Post

baavgai

RE: Bubble Sort Logic

20 May, 2009 - 01:58 PM
Post #6

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 4,261



Thanked: 389 times
Dream Kudos: 550
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
QUOTE(Jubb @ 20 May, 2009 - 03:19 PM) *

It's only inefficient if you actually have to sort the data! Or if you have a large amount of data. Other than that we're looking at a best case complexity of O(n) !!


Thanks, I'd meant to respond to that. tongue.gif

Put another way, if your list is mostly in order, with a handful of data points out of place, bubble sort can be the most effective. Most sorts are exhaustive, needing to do at least size*size compares. The humble bubble sort, for all it's simplicity, can beat a quick sort given best case data.

This post has been edited by baavgai: 20 May, 2009 - 02:02 PM
User is online!Profile CardPM
+Quote Post

dsherohman

RE: Bubble Sort Logic

21 May, 2009 - 02:17 AM
Post #7

D.I.C Head
**

Joined: 29 Mar, 2009
Posts: 184



Thanked: 35 times
My Contributions
QUOTE(baavgai @ 20 May, 2009 - 09:58 PM) *

Put another way, if your list is mostly in order, with a handful of data points out of place, bubble sort can be the most effective. Most sorts are exhaustive, needing to do at least size*size compares. The humble bubble sort, for all it's simplicity, can beat a quick sort given best case data.

Or given a small enough data set.

There's a common misconception that big-O notation describes run time, but it does not. It describes how run time increases as the data set grows, but there is also a constant multiplier involved. In this case, that constant primarily reflects the amount of effort that goes into actually performing each comparison.

For example, let's consider a bubble sort (average-case complexity O(n^2), taking 1 unit of time to perform each comparison) vs. a quicksort (average-case O(n log n), taking 100 units of time to perform each comparison). So, on average, the bubble sort will require n^2 time units to complete, while the quicksort will take 100n log n. Solving for n, we find that the bubble sort is faster for n < 237 (assuming base 10 log) or n < 647 (assuming natural log).

I the real world, I wouldn't expect the constant to be a 100-to-1 difference, but the constant factor is lower for bubble sort than any other.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 04:44AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month