Results 1 to 3 of 3
- 03-02-2013, 10:54 AM #1
Member
- Join Date
- Mar 2013
- Posts
- 1
- Rep Power
- 0
How to understand Bubble Sort Pseudo code?
Hi,
I have difficulity understanding the Pseudo code compared to Java, i had cracked my head but still dont understand the coding, just a few lines but i couldn't get myself to fully understand.
The Pseudo Code:
for i=1 to n-1 do
for j=i to n-1 do
if x[j]>x[j+1] then
temp=x[j]
x[j]=x[j+1]
x[j+1]=temp
end {if}
end {for}
end {for}
From my understanding of this bubble sort code:
int[] x = {5, 4, 3, 2, 1};
int n = x.length;
int temp;
int counter = 0;
for (int i = 0; i < n; i++) {
// counter ++;
for (int j = i; j < n; j++) {
if (x[j] > x[j + 1]) {
temp = x[j];
x[j] = x[j + 1];
x[j + 1] = temp;
counter++;
}
}
}
for (int i = 0; i < x.length; i++) {
System.out.print(x[i] + " ");
}
System.out.println();
System.out.println(counter);
My question is:
Is the Pseudo Code same as the other bubble sort code online? This do not have the true/false checker.
I need to calculate the:
1) Best case
2) Worst case
3) Average case
for the pseudo code, in terms of Big O Notation, all bubble sort should be worst case Big O N square, best is Big O(n), average is same as worst case.
However i am unable to convince myself by looking at the pseudo code alone, any kind souls please assist if you understand the Pseudo code, Thanks in advance
- 03-02-2013, 01:54 PM #2
Re: How to understand Bubble Sort Pseudo code?
Why do they call it rush hour when nothing moves? - Robin Williams
- 03-02-2013, 02:33 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
Re: How to understand Bubble Sort Pseudo code?
When people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Bubble Sort For 2D Arrays
By tmantonym in forum New To JavaReplies: 2Last Post: 11-30-2011, 09:40 PM -
Problem with bubble sort code
By flyingcurry in forum New To JavaReplies: 4Last Post: 11-11-2010, 03:14 AM -
Bubble sort
By pineapple in forum New To JavaReplies: 3Last Post: 04-25-2009, 12:45 AM -
How to sort a list using Bubble sort algorithm
By Java Tip in forum AlgorithmsReplies: 3Last Post: 04-29-2008, 08:04 PM -
need help with bubble sort
By lowpro in forum New To JavaReplies: 3Last Post: 12-17-2007, 05:27 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks