# Shortest Job First Sorting

Page 1 of 1

## 3 Replies - 7665 Views - Last Post: 15 August 2012 - 02:00 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=288853&amp;s=c45b4bce210ce9e2b717f73c49a29c2b&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 k3nnt0ter0

Reputation: 1
• Posts: 188
• Joined: 16-January 10

# Shortest Job First Sorting

Posted 14 August 2012 - 09:21 AM

I am making a CPU Scheduling Shortest Job First and I manage to compute it. My problem is how to arrange the Horizontal Aligned JOBS (The one at the top) from lowest to highest.

Check the Image :

Like for example, Job 3 is the Lowest so it comes first next is Job 5 and so on.

Here's the Code :
```Public Class frmSJF

Public Function NumberSorting(ByVal numbers() As Integer) _
As Integer()

For j As Integer = 0 To numbers.Length - 1

For x As Integer = 0 To numbers.Length - 1
If numbers(x) > numbers(j) Then
numbers(x) = numbers(x) + numbers(j)
numbers(j) = numbers(x) - numbers(j)
numbers(x) = numbers(x) - numbers(j)
End If
Next
Next

Return numbers

End Function

Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click

If tbBT4.Text = "" And tbBT5.Text = "" Then

Dim numbers() As Integer = {Val(tbBT1.Text), Val(tbBT2.Text), Val(tbBT3.Text)}
Dim sortedNumberList() As Integer = NumberSorting(numbers)
Dim ans1(3) As Integer

ans1(0) = sortedNumberList(0) + 0
ans1(1) = sortedNumberList(1) + ans1(0)
ans1(2) = sortedNumberList(2) + ans1(1)

Label1.Text = 0
Label2.Text = ans1(0)
Label3.Text = ans1(1)
Label4.Text = ans1(2)

lblCompAWT.Text = "0" + " + " + ans1(0).ToString + " + " + ans1(1).ToString
lblCompAWTAns.Text = (0 + ans1(0) + ans1(1)).ToString + " / " + "3"
lblTAWT.Text = ((0 + ans1(0) + ans1(1)) / 3).ToString + " ms"

lblCompATAT.Text = ans1(0).ToString + " + " + ans1(1).ToString + " + " + ans1(2).ToString()
lblCompATATAns.Text = (ans1(0) + ans1(1) + ans1(2)).ToString + " / " + "3"
lblTATAT.Text = ((ans1(0) + ans1(1) + ans1(2)) / 3).ToString + " ms"

ElseIf tbBT5.Text = "" Then

Dim numbers() As Integer = {Val(tbBT1.Text), Val(tbBT2.Text), Val(tbBT3.Text), Val(tbBT4.Text)}
Dim sortedNumberList() As Integer = NumberSorting(numbers)
Dim ans2(4) As Integer

ans2(0) = sortedNumberList(0) + 0
ans2(1) = sortedNumberList(1) + ans2(0)
ans2(2) = sortedNumberList(2) + ans2(1)

Label1.Text = 0
Label2.Text = ans2(0)
Label3.Text = ans2(1)
Label4.Text = ans2(2)

lblCompAWT.Text = "0" + " + " + ans2(0).ToString + " + " + ans2(1).ToString + " + " + ans2(2).ToString()
lblCompAWTAns.Text = (0 + ans2(0) + ans2(1) + ans2(2)).ToString + " / " + "4"
lblTAWT.Text = ((0 + ans2(0) + ans2(1) + ans2(2)) / 4).ToString + " ms"

lblCompATAT.Text = ans2(0).ToString + " + " + ans2(1).ToString + " + " + ans2(2).ToString() + " + " + ans2(3).ToString
lblCompATATAns.Text = (ans2(0) + ans2(1) + ans2(2) + ans2(3)).ToString + " / " + "4"
lblTATAT.Text = ((ans2(0) + ans2(1) + ans2(2) + ans2(3)) / 5).ToString + " ms"

ElseIf tbBT4.Text = "" Then

MsgBox("Do not leave this Blank")

Else

Dim numbers() As Integer = {Val(tbBT1.Text), Val(tbBT2.Text), Val(tbBT3.Text), _
Val(tbBT4.Text), Val(tbBT5.Text)}
Dim sortedNumberList() As Integer = NumberSorting(numbers)
Dim ans(5) As Double

ans(0) = sortedNumberList(0) + 0
ans(1) = sortedNumberList(1) + ans(0)
ans(2) = sortedNumberList(2) + ans(1)
ans(3) = sortedNumberList(3) + ans(2)
ans(4) = sortedNumberList(4) + ans(3)

Label1.Text = 0
Label2.Text = ans(0)
Label3.Text = ans(1)
Label4.Text = ans(2)
Label5.Text = ans(3)
Label6.Text = ans(4)

lblCompAWT.Text = "0" + " + " + ans(0).ToString + " + " + ans(1).ToString + " + " + ans(2).ToString() + " + " + ans(3).ToString
lblCompAWTAns.Text = (0 + ans(0) + ans(1) + ans(2) + ans(3)).ToString + " / " + "5"
lblTAWT.Text = ((0 + ans(0) + ans(1) + ans(2) + ans(3)) / 5).ToString + " ms"

lblCompATAT.Text = ans(0).ToString + " + " + ans(1).ToString + " + " + ans(2).ToString() + " + " + ans(3).ToString + " + " + ans(4).ToString
lblCompATATAns.Text = (ans(0) + ans(1) + ans(2) + ans(3) + ans(4)).ToString + " / " + "5"
lblTATAT.Text = ((ans(0) + ans(1) + ans(2) + ans(3) + ans(4)) / 5).ToString + " ms"

End If
End Sub
End Class
```

Is This A Good Question/Topic? 0

## Replies To: Shortest Job First Sorting

• MrCupOfT

Reputation: 2298
• Posts: 9,535
• Joined: 29-May 08

## Re: Shortest Job First Sorting

Posted 14 August 2012 - 10:14 AM

Your method of swapping the contents of two elements is brittle, because numbers can overflow. An overflow is an exception.

A better method of swapping is not to use arithmetic based approach.

```Public Module Exts

<System.Runtime.CompilerServices.Extension()>
Public Sub SwapWith(Of T)(ByRef X As T, ByRef Y As T) As T
Dim Tmp = X
X = Y
Y = Tmp
End Sub

End Module

```

This swap method work for any type. It utilises generic parameters, (the T aspect). Once you use on an instance, the type of the second parameter will automagically be restrict to the same type as the first, or a derived one.

The <System.Runtime.CompilerServices.Extension()> unables the methods to appear if it work coded into type class

Example
```Dim x= "A"
Dim y = "B"
x.SwapWith(y)
Dim a = 12
Dim b = 100
a.SwapWith(B)/> ' Same method called, but on a different type.

```

Which I think is cool, as it saves me having to write one for each class I can possibly conceive.

Pro Tip: Give your controls and variables sensible and meaningful name that convey their context and usage. Imagine if lots and lots controls and they only differ in their name by the number on the end. Eg Textbox1 .. TextBox42 etc, Or tb_UserName ... tb_Password

Also rarely any need to write your one sorting routine, since you could utilise the .net framework in-built ones. Or use LINQ to do the sort ordering for you. Tutorial: Custom Sorting

Don't assume the user will in valid data. Validate it before you use it.

This post has been edited by AdamSpeight2008: 14 August 2012 - 10:17 AM

### #3 k3nnt0ter0

Reputation: 1
• Posts: 188
• Joined: 16-January 10

## Re: Shortest Job First Sorting

Posted 14 August 2012 - 11:50 PM

AdamSpeight2008, on 14 August 2012 - 11:14 AM, said:

Your method of swapping the contents of two elements is brittle, because numbers can overflow. An overflow is an exception.

A better method of swapping is not to use arithmetic based approach.

```Public Module Exts

<System.Runtime.CompilerServices.Extension()>
Public Sub SwapWith(Of T)(ByRef X As T, ByRef Y As T) As T
Dim Tmp = X
X = Y
Y = Tmp
End Sub

End Module

```

This swap method work for any type. It utilises generic parameters, (the T aspect). Once you use on an instance, the type of the second parameter will automagically be restrict to the same type as the first, or a derived one.

The <System.Runtime.CompilerServices.Extension()> unables the methods to appear if it work coded into type class

Example
```Dim x= "A"
Dim y = "B"
x.SwapWith(y)
Dim a = 12
Dim b = 100
a.SwapWith(B)/> ' Same method called, but on a different type.

```

Which I think is cool, as it saves me having to write one for each class I can possibly conceive.

Pro Tip: Give your controls and variables sensible and meaningful name that convey their context and usage. Imagine if lots and lots controls and they only differ in their name by the number on the end. Eg Textbox1 .. TextBox42 etc, Or tb_UserName ... tb_Password

Also rarely any need to write your one sorting routine, since you could utilise the .net framework in-built ones. Or use LINQ to do the sort ordering for you. Tutorial: Custom Sorting

Don't assume the user will in valid data. Validate it before you use it.

Hehehe. My Professors are always saying about the Naming of Controls and I do follow proper naming depending on how many controls are present in the form or depending on the program I am making. Some controls falls under the the same category so I just name them the same but differs on the number in the end. Anyway thanks for reminding me

You were referring on the way of swapping of my numbers, aren't you? I also find it quite complicated in my part so I guess I'll try those Custom Sorting and the one you suggested. But I don't have idea how to swap those Horizontal Aligned JOBS.

Like for example, Job4 is the lowest so it comes first. Next is Job3 and so on. I want to swap them also I find it difficult because it was a control that needs to be swapped. Swapping numbers are less difficult than swapping controls.

• MrCupOfT

Reputation: 2298
• Posts: 9,535
• Joined: 29-May 08

## Re: Shortest Job First Sorting

Posted 15 August 2012 - 02:00 PM

In .net there essentially two classifications of type.
ValueType
RefType

Explained better in this fantastic tutorial by crepitus

Remember I said my swap could handle any type. Just imagine T as a placeholder for type.
```Public Module Exts

<System.Runtime.CompilerServices.Extension()>
Public Sub SwapWith(Of T)(ByRef X As T, ByRef Y As T) As T
Dim Tmp = X  ' Tmps is now the value of X or pointing to X (Tmp is inferred to be of Type T) '
X = Y        ' X is now the value of Y or pointing to Y '
Y = Tmp      ' Y is now the value of / pointer to Tmp (the original X)
End Sub

End Module

```

This post has been edited by AdamSpeight2008: 15 August 2012 - 02:05 PM