Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Visual Basic (http://www.programmingforums.org/forum18.html)
-   -   Degree project help! Sorting an array (http://www.programmingforums.org/showthread.php?t=14556)

sepalm Nov 22nd, 2007 11:07 AM

Degree project help! Sorting an array
 
Right, We were set the task of designing a program for a fictional company "Loud music". I've come to a point in the code which i've never crossed before, i have to sort my array into ascending order. I've no idea how to sort an array, i've looked at numerous tutorials on numerous websites, and come to the conclusion bubble sort would be best? but can't apply it. This is the code:

:

Dim totalbr0(1 To 8) As Single

Private Sub cmdsearch_Click()
totalbr0(1) = 0
totalbr0(2) = 0
totalbr0(3) = 0
totalbr0(4) = 0
totalbr0(5) = 0
totalbr0(6) = 0
totalbr0(7) = 0
totalbr0(8) = 0
Dim startweek As Integer
Dim endweek As Integer
If txtweek1.Text = "" Then
    txtweek1.Text = txtweek.Text
End If
startweek = Val(txtweek.Text)
endweek = Val(txtweek1.Text)
Dim filename As String
filename = App.Path & "\productdata.csv"
Open filename For Input As #1
 Do
    Input #1, branch, week, prod, Item, gen, sales
    Dim fileweek As Integer
    fileweek = Val(week)
    If fileweek >= startweek And fileweek <= endweek Then
        If branch = "br01" Then
          totalbr0(1) = totalbr0(1) + sales
        End If
        If branch = "br02" Then
            totalbr0(2) = totalbr0(2) + sales
        End If
        If branch = "br03" Then
            totalbr0(3) = totalbr0(3) + sales
        End If
        If branch = "br04" Then
            totalbr0(4) = totalbr0(4) + sales
        End If
        If branch = "br05" Then
          totalbr0(5) = totalbr0(5) + sales
        End If
        If branch = "br06" Then
          totalbr0(6) = totalbr0(6) + sales
        End If
        If branch = "br07" Then
          totalbr0(7) = totalbr0(7) + sales
        End If
        If branch = "br08" Then
          totalbr0(8) = totalbr0(8) + sales
        End If
    End If
Loop Until EOF(1)
Close #1
End Sub


I'm not asking anyone to do my "schoolwork" for me, i'm hoping someone will be able to lay out what i'd have to do in lamens terms because im a novice programmer, and up to now the program was going quite well. Thankyou.

DaWei Nov 22nd, 2007 11:39 AM

Re: Degree project help! Sorting an array
 
Twenty-five ways.

sepalm Nov 22nd, 2007 11:58 AM

Re: Degree project help! Sorting an array
 
Quote:

Originally Posted by DaWei (Post 137360)

I appreciate that, and im sorry but i didn't understand it. if someone could just write a standardised bubblesort in pseudo code i might be able to work it out. Thanks.

kruptof Nov 22nd, 2007 11:59 AM

Re: Degree project help! Sorting an array
 
Quote:

Originally Posted by sepalm (Post 137358)
conclusion bubble sort would be best?

How did you arrive at that?

DaWei Nov 22nd, 2007 12:07 PM

Re: Degree project help! Sorting an array
 
So you only want us to do HALF your work for you? How about an explanation, instead. No frills for optimization.

Compare the first element to every other element in the array. If the compared element is smaller, swap 'em and continue. This will bubble the smallest item to the top.

Move to the second element. Come it to every remaining element in the array. Swap if necessary.

Rinse and repeat until done.

For a descending sort, reverse the nature of the comparison.

sepalm Nov 22nd, 2007 12:17 PM

Re: Degree project help! Sorting an array
 
how is explaining the concept of a bubble sort and how it's done DOING half the work for me?

Thankyou for the explanation, it made more sense than most of the tutorials that i've looked at on the internet. I'll attempt to apply it.

DaWei Nov 22nd, 2007 12:28 PM

Re: Degree project help! Sorting an array
 
Writing pseudocode, as you asked, would be doing half the work for you. You'd only need a translator, not a thought process.

sepalm Nov 22nd, 2007 12:32 PM

Re: Degree project help! Sorting an array
 
by writing pseudo code i literally just meant, could you write the standard form of a bubble sort

for example an if statement as
if (condition, statement)

i don't want someone to literally just write the code so i can copy it, that's of no use to me. I want to learn it, because without learning it i could be questioned on it and not know what i'm talking about.

null_ptr0 Nov 22nd, 2007 12:36 PM

Re: Degree project help! Sorting an array
 
Quote:

Originally Posted by wikipedia
:

  1. procedure bubbleSort( A : list of sortable items ) defined as:
  2.   for each i in 1 to length(A) do:
  3.     for each j in length(A) downto i + 1 do:
  4.       if A[ j -1  ] > A[ j ] then
  5.         swap( A[ j - 1],  A[ j ] )
  6.       end if
  7.     end for
  8.   end for
  9. end procedure


Quote:

Originally Posted by paked.net
:

  1. #include <iostream.h>
  2.  
  3. int BubbleSort(int [], int);
  4.  
  5. int main()
  6. {
  7. const int NUMEL = 10;
  8. int nums[NUMEL] = {22,5,67,98,45,32,101,99,73,10};
  9. int i, moves;
  10.  
  11. moves = BubbleSort(nums, NUMEL);
  12.  
  13. cout << "The sorted list, in ascending order, is:\n";
  14. for (i = 0; i < NUMEL; ++i)
  15. cout << " " <<nums[i];
  16.  
  17. cout << '\n' << moves << " were made to sort this list\n";
  18.  
  19. return 0;
  20. }
  21.  
  22. int BubbleSort(int num[], int numel)
  23. {
  24. int i, j, grade, moves = 0;
  25.  
  26. for ( i = 0; i < (numel - 1); i++)
  27. {
  28. for(j = 1; j < numel; j++)
  29. {
  30.  
  31. if (num[j] < num[j-1])
  32. {
  33.         grade = num[j];
  34.         num[j] = num[j-1];
  35.         num[j-1] = grade;
  36.         moves++;
  37.                 }
  38.         }
  39. }
  40.  
  41. return moves;
  42. }


Really, just search these things up.

sepalm Nov 22nd, 2007 6:33 PM

Re: Degree project help! Sorting an array
 
Managed to get it working, and now i've done it i actually understand a bubble sort.

Thanks dawei and null_ptr0


All times are GMT -5. The time now is 3:36 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC