0

Trouble with sorting!

Recently, I was tasked with implementing the cocktail sorting algorithm into a class and I am having a problem with the sorting aspect itself, I've looked at other examples of code (not in the form of a class) to help me with writing it but I am still stuck. https://code.sololearn.com/c8FOA8BYRI9b/?ref=app.

16th Apr 2020, 11:46 AM
Oldie
31 Antworten
+ 1
https://code.sololearn.com/cF1us120URyL/?ref=app
16th Apr 2020, 2:07 PM
William M
William M - avatar
0
Look at my page code -> sort(corrected) -> I made your code work
16th Apr 2020, 2:05 PM
William M
William M - avatar
0
Thank you!!
16th Apr 2020, 10:57 PM
Oldie
0
You are welcome!
16th Apr 2020, 10:58 PM
William M
William M - avatar
0
I do have a question (possibly questions) though, the first being do the changes you made still conform to the cocktail sorting algorithm?
16th Apr 2020, 11:26 PM
Oldie
0
Also, I don't really understand how the two for loops work
16th Apr 2020, 11:27 PM
Oldie
0
Oh and lastly (for now at least), why is cin >> v;//+++ array[l] = v;//+++ better than cin >> array{l}
16th Apr 2020, 11:29 PM
Oldie
0
To the first question, yes they still conform. To the second question, why two for loops? Assume you have an unsorted array b with 4 elements. int b[4] = {5,1,7,4} One for loop would do one round with the range of 5. 5 compared with 1(swap), 5 with 7, 7 with 4(swap) Result? b is now : {1,5,4,7} It still not completely unsorted right? Let us do another round with range 5. 1 compared with 5, 5 with 4(swap), 5 with 7 how to reach more rounds in rounds. for loop in a for loop. b is now {1,4,5,7} but with other bigger lists you will always need to do as many rounds as the length of the array, so that the sorting algorithm does not overlook any element.
16th Apr 2020, 11:37 PM
William M
William M - avatar
0
To the 3rd question. I did because I just wanted to be sure that all possible small mistakes (for me) are not in my way. Otherwise you could use your style of doing it.
16th Apr 2020, 11:40 PM
William M
William M - avatar
0
sorry if I'm taking your time but with the for loop, when it's going multiple rounds, doesn't it only do so in one direction and not in both directions? for example: b[4] = {5, 1, 7, 4} after it swaps and then the list becomes b[4] = {1, 5, 4, 7}, shouldn't it start swapping from the back i.e 7 compared with 4, then 4 compared with 5 (swap)?
16th Apr 2020, 11:44 PM
Oldie
0
Yes you right. I swithed the code to a bubble sort :( sorry. But I can fix it
16th Apr 2020, 11:55 PM
William M
William M - avatar
0
And no you are not taking of my time :)
16th Apr 2020, 11:55 PM
William M
William M - avatar
0
alright cool, I'll be here
16th Apr 2020, 11:56 PM
Oldie
0
oh by the way I was able to edit the original code I had sent so that it kinda works better but it still has some problems https://code.sololearn.com/cQ6Lw5EeIceA/?ref=app
17th Apr 2020, 12:13 AM
Oldie
0
Oh great. But you have to define a function sort.
17th Apr 2020, 12:15 AM
William M
William M - avatar
0
I did define a sort function, it's called cocktailsort
17th Apr 2020, 12:17 AM
Oldie
0
In line 58 you should type c1.cocktailsort() instead of c1.sort()
17th Apr 2020, 12:20 AM
William M
William M - avatar
0
it still "works", the problem is out of the 8 numbers I inputted, only 7 were sorted, the 8th dissappeared
17th Apr 2020, 12:31 AM
Oldie
0
in line 14 for(..; i< end;..) without the equal sign between i and end in line 36 for(..; i > head -1;..) also no equal sign between i and head and head-1
17th Apr 2020, 12:40 AM
William M
William M - avatar
0
I tried it (while keeping the equal sign between i and head) and it works! If I take away the equal sign every second element that I enter into my array comes 1st and then all the other elements are sorted
17th Apr 2020, 12:45 AM
Oldie