This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2013-03-19 16:26:46

lolakg
New Scratcher
Registered: 2013-03-19
Posts: 2

Bubble Sort Project

Hey! So I am new to scratch, and working on a project to create a function "bubble sort" which sorts any given list L, it should be able to sort any list of any random length.
I am having some problems with this and I was wondering if anyone had any ideas that might be helpful?

Offline

 

#2 2013-03-19 17:18:30

lolakg
New Scratcher
Registered: 2013-03-19
Posts: 2

Re: Bubble Sort Project

[scratchblocks]
when gf clicked
set [pass v] to [0]
set [swaps v] to [0]
repeat until <<(pass) > [0]> and <(swaps) = [0]>>
  set [item v] to [0]
  change [pass v] by (1)
  set [swaps v] to [0]
  repeat ((length of [data v]) - (1))
    change [item v] by (1)
    if <(item ((item) + (1)) of [data v]) < (item (item) of [data v])>
      set [value v] to (item ((item) + (1)) of [data v])
      replace item ((item) + (1)) of [data v] with (item (item) of [data v])
      replace item (item) of [data v] with (value)
      change [swaps v] by (1)
    end
  end
end

Offline

 

#3 2013-03-19 17:20:19

Hyperbola
Scratcher
Registered: 2013-03-15
Posts: 100+

Re: Bubble Sort Project

lolakg wrote:

when gf clicked
set [pass v] to [0]
set [swaps v] to [0]
repeat until <<(pass) > [0]> and <(swaps) = [0]>>
  set [item v] to [0]
  change [pass v] by (1)
  set [swaps v] to [0]
  repeat ((length of [data v]) - (1))
    change [item v] by (1)
    if <(item ((item) + (1)) of [data v]) < (item (item) of [data v])>
      set [value v] to (item ((item) + (1)) of [data v])
      replace item ((item) + (1)) of [data v] with (item (item) of [data v])
      replace item (item) of [data v] with (value)
      change [swaps v] by (1)
    end
  end
end

Fixed.


Tip of the whatever: Don't post in threads older than 2 weeks unless your post will actually be useful.
It's the last day of 1.4! *cries* (quote from NeilWest, rest by me) by the time you read this it probably will be 2.0  sad

Offline

 

#4 2013-03-19 17:24:32

SarkyKeshy
New Scratcher
Registered: 2013-02-13
Posts: 9

Re: Bubble Sort Project

Bubble sort is here using the List Bubble sort. not sure you can make it take a list as a variable even in Scratch 2.0 where the variables seem to be numerics or text rather than objects.
http://scratch.mit.edu/projects/SarkyKeshy/3190837

Offline

 

Board footer