View Single Post
Old 07-18-2016, 04:23 PM   #7
Dinglesberry
longing
FFR Veteran
 
Dinglesberry's Avatar
 
Join Date: Dec 2007
Location: Ontario, Canada
Posts: 2,680
Default Re: programming question

Bro check this out too, for example you do, right inside the bubble sort method, "int temp" (it's like the first line of the function). You use an int to save the current value of the array (so you can put it back to the right spot after you swap), but the arrays are now chars, are they not?

This is just one example of ensuring type is correct, and it's VERY important in type safe languages like java, c++ etc. you might enjoy PHP if you aren't a fan of this, but you should wait until you are more familiar with objects and such before starting php

EDIT: I'm on my PC now, was on my phone earlier, I'm going to point out specifically what I mean, in this case, and I'll try to explain the bubble sort for you:

Code:
void Bubblesort(int unlist[], int sortlist[], int numberOfElements)
{
int temp;
for (int i = 0; i < numberOfElements; i++)
sortlist[i] = unlist[i];

for (int i = 0; i < numberOfElements; i++)
{
for (int j = 0; j<numberOfElements - 1; j++)
{
if (sortlist[j]>sortlist[j + 1])
{
temp = sortlist[j];
sortlist[j] = sortlist[j + 1];
sortlist[j + 1] = temp;
I'm bolding the key part for you. What you are doing is, you pass the algorithm an unsorted array, as well as an array to hold the sorted list, which I'm going to assume is just an empty array of equal size (might even be just a copy, would have the same result). EDIT: sortlist[i] = unlist[i]; it is a copy, which asks the question why you would have to pass the function a sorted list at all instead of just creating one inside the function itself, but that's not really relevant, works either way...

Bubblesort is not the most efficient algorithm, but what you are doing is: You go through every element of the list, 0 to list.length, one by one. For each of them, you check if the current element (sortlist[j]) is greater than the NEXT thing in the array (sortlist[j + 1]). If it is, lets say [ 3, 2 .... ] (and we are looking at 3), obviously you need to switch their position, so:

1. You save the current value (the element you are looking at... remember, we are stepping through the array, item by item) temp = sortlist[j];
2. You swap the values: We set the current thing we are looking at to the value of the NEXT thing (e.g. [ 3, 2 ...] becomes [ 2, 2 ....].
3. OH SHIT, WHAT HAPPENED TO THE 3!?! OH WAIT, WE SAVED IT IN temp = sortlist[j];! BOOM, we set the NEXT element, j + 1, to whatever we saved in temp - sortlist[j + 1] = temp;
4. Sweet, now the array is like [ 2, 3... ], now we step forward and are comparing the next element.. [2, 3, 6 ....], since we did j++ in the loop, we are comparing 3 and 6 etc...

The bubble sort goes through the entire array.

When you think about how the algorithm works, its honestly trivial what data type the array sorts. Lets say the array is [ 'a', 'g', 's', 'b' ]... the comparisons and the way it executes would be the exact same, because a value like a is really just a number (lets arbitrarily say its like 56). If a is 56, then b is 57, etc.

Last edited by Dinglesberry; 07-18-2016 at 04:40 PM..
Dinglesberry is offline   Reply With Quote