Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

pseudocode selection sort

pseudocode

  • Please log in to reply
5 replies to this topic

#1 dillyw82

dillyw82

    CC Newcomer

  • Member
  • PipPip
  • 15 posts
  • Programming Language:JavaScript
  • Learning:JavaScript

Posted 22 October 2012 - 01:46 AM

hi i am new to this site so i hope i have posted this in the right place

This is for a assignment, I just want some one to double check before submission please i submitted the top code but my teacher sent it back with the following remarks I have a rewritten version below but as this is my final hange for submission I was hoping some one could please proof it for me
Selection sort pseudo-code
The outer loop repeats the selection process n-1 times. For each iteration it is initially assumed that the first entry (index 0) contains the highest value (max = 0). The inner loop then examines the array from left to right and whenever a higher value is found, it's locations in the list is stored in the value "max".
At the end of the first iteration, the last entry in the list has it's value swapped with the entry containing the highest value. The counter of the outer loop is then decremented

For K= 0 to n-1 Max= 0 For J = 0 to n-K-1 If item (J) > Item (Max) Max = Item (J) End If End For Swap item n-k-1 with Max End for End
Your code has the following expression:
If item (J) > Item (Max)
Max = Item (J)
End If

The algorithm requires the variable “Max” to hold the location or position in the array of the next highest value to be swapped. You are assigning to Max the value stored inside that location in the array.
You also need to show step-by-step how the value stored in Item[n-k-1] with the value stored in location Max. This is requested because data can be lost if a swap is not fully understood.
I have since changed it to
for K = 0 to n-1
Max = 0
for j= 0 to n - K-1
if Num[j]> Num[Max]
Max = j
End if
End for
temp = Num[n-k-1]
Num[n-k-1] = Num[Max]
Num[Max] = temp
End for

sorry dont know why the code is all on 1 line there
  • 0

#2 lintwurm

lintwurm

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 261 posts
  • Programming Language:C, Java, C++, C#, PHP, (Visual) Basic, Python, Visual Basic .NET

Posted 22 October 2012 - 03:40 AM

Use Code tags even if it is pseudocode :P anyways... your code seems valid, but I just gave it a quick once-over... The only problem might be that you declared K as capital, but changed to lowercase k further down... I don't know how strict your markers may be...
  • 0
No harm was done to the world in sending this message, but a few million electrons have been SERIOUSLY inconvenienced...

#3 dillyw82

dillyw82

    CC Newcomer

  • Member
  • PipPip
  • 15 posts
  • Programming Language:JavaScript
  • Learning:JavaScript

Posted 22 October 2012 - 12:54 PM

thanks for looking it over :) much appreciated, sorry about the tags i thought i had taged it as homwork but must not of worked,
  • 0

#4 lintwurm

lintwurm

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 261 posts
  • Programming Language:C, Java, C++, C#, PHP, (Visual) Basic, Python, Visual Basic .NET

Posted 22 October 2012 - 10:27 PM

Not those kind of tags :D

I meant like this:
for(int i = 0 ; i < 50 ; i++)
{
    cout << "I like apples" << endl;
}

See the Code tags? :P
  • 0
No harm was done to the world in sending this message, but a few million electrons have been SERIOUSLY inconvenienced...

#5 lespauled

lespauled

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1360 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash

Posted 23 October 2012 - 05:35 AM

He was talking about code tags as in : [ CODE ] your code here and [ / CODE ] tags (without the spaces). The button on the editor looks like <>. If just makes things more readable by putting it in a different color background and uses a fixed size font.

So, your code would look like this:

for K = 0 to n-1
Max = 0
for j= 0 to n - K-1
	  if Num[j]> Num[Max]
			Max = j
	  End if
End for
temp = Num[n-k-1]
Num[n-k-1] = Num[Max]
Num[Max] = temp
End for


Nothing major, just easier on the eyes.
  • 0
My Blog: http://forum.codecal...699-blog-77241/
"Women and Music: I'm always amazed by other people's choices." - David Lee Roth

#6 dillyw82

dillyw82

    CC Newcomer

  • Member
  • PipPip
  • 15 posts
  • Programming Language:JavaScript
  • Learning:JavaScript

Posted 23 October 2012 - 01:27 PM

Oh ok thanks, passed the assessment thanks for every ones help,
  • 0





Also tagged with one or more of these keywords: pseudocode

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download