Are you a moron? He's trying to do bubble sort with a single for loop. It's just an algorithm problem not something a stack trace can fix my good god you are stupider than my dick
Evan Lopez
are you trying to sort? Me thinks you need to look at how minmax algo looks like
James Turner
Yes I know it's a bubble sort but for all I know he forgot a semi colon somewhere. It's easier to debug a stacktrace rather than code.
Benjamin Hill
"Doesn't work" in this case isn't a compiler error actually. Just realized that. He needs two for loops lol
Isaiah Russell
Look at the for loop doing the swapping.
Write out an example, and increment 'i' as you go. Look at what happens when it does its final comparison of the value of 'i'.
Your for loops all technically iterate the same number of times. You should iterate up to 'i < size - 1' (not
Liam Murphy
You get this iteration. if (num[7] > num[8])
Austin Williams
because you fucked up code tags
Zachary Thompson
>for (i = 0; i < size; i++) but also >for (i = 0; i
Jacob Gomez
Please no... just use while loops and a flag to see when it's sorted. Also, maybe abstract out the three line swap into a swap(int&,int&) function as they are clearly a beginner and it will be easier to focus on the loop. Should be something like this, but in actual code, not psuedo:
bool notSorted; while(notSorted==true) { notSorted=false; for(size-1) if(element and Element+1 need swapping) { swap(element, element+1) notSorted=true; } }
That should be more than enough to help you figure it out. If you still don't get it, just drop the course, you'll never make it if you can't even bubble sort. Also, youtube and stackoverflow answers have literally hundreds of solutions to this. Jow Forums is not the right place to ask. There might be a minor error in my algo, I didn't run it obviously, but I would start by trying to understand those parts as that seems to be where you are having trouble...
op wants: >for (i = 0; i < size -1; i++) because you always compare to numbers at a time. if you do this for each element the last one will check the element against an out of bounds element noob.
Also, OP, you do >const int size = 8; >int num[size], numSorted[8]; but also >cout
You're trying to do a bubble sort, which is O(N^2) with a single iteration, which is O(N). And seeing as you didn't just reinvent computer science as we know it your algorithm is broken
Grayson Moore
You don't need >#include That's for dynamic arrays that change size...
>No one is this stupid... Imagine being SO smart to watch, store and share disgusting poorly drawed pedo cartoons.
Owen Long
Some people can't help it. It's reflexive. Something like this is so simple it's solved in seconds and posted before you even realize what you've done. It's like watching someone struggle with single digit arithmetic... It's almost harder to not solve it.
Oof! Needs 7 passes to cover worst case scenario. Smaller values 'bubble up' to the top of the list with each pass Oh well... homework is homework.
// Revison: added variable k because I'm bored (and lazy). // Note: never use bubble sort for anything other than homework. // Note2: never use bubblesort outside of community college. Even then, it should remain within the confines of the Liberal Arts. // War Emergency: Use quicksort to partition down to stacks of 5-8 elements and shellsort each stack in sequence.
#include #include using namespace std;
int main (void) { int i, k, temp; int count = 0, mode = 0; const int size = 8; int num[size], numSorted[8];
cout > num[i];
for (k = 0; k < 7; ++k) { for (i = 0; i num[i + 1]) { temp = num[i]; num[i] = num[i + 1]; num[i + 1] = temp; } } } cout
Luis Baker
ugh... using a for loop means this is always worst case....