Add tasks for all the new languages

This commit is contained in:
Tina Müller 2016-12-05 23:44:36 +01:00
parent 9dc3c2bb62
commit bba7bfd280
13208 changed files with 134745 additions and 0 deletions

View file

@ -0,0 +1,36 @@
#import <Foundation/Foundation.h>
void quicksortInPlace(MutableArray array, const long first, const long last)
if first >= last
return
Value pivot = array[(first + last) / 2]
left := first
right := last
while left <= right
while array[left] < pivot
left++
while array[right] > pivot
right--
if left <= right
array.exchangeObjectAtIndex: left++, withObjectAtIndex: right--
quicksortInPlace(array, first, right)
quicksortInPlace(array, left, last)
Array quicksort(Array unsorted)
a := []
a.addObjectsFromArray: unsorted
quicksortInPlace(a, 0, a.count - 1)
return a
int main(int argc, const char * argv[])
autoreleasepool
a := [1, 3, 5, 7, 9, 8, 6, 4, 2]
Log( 'Unsorted: %@', a)
Log( 'Sorted: %@', quicksort(a) )
b := ['Emil', 'Peg', 'Helen', 'Juergen', 'David', 'Rick', 'Barb', 'Mike', 'Tom']
Log( 'Unsorted: %@', b)
Log( 'Sorted: %@', quicksort(b) )
return 0

View file

@ -0,0 +1,33 @@
#import <Foundation/Foundation.h>
implementation Array (Quicksort)
plus: Array array, return Array =
self.arrayByAddingObjectsFromArray: array
filter: BOOL (^)(id) predicate, return Array
array := []
for id item in self
if predicate(item)
array.addObject: item
return array.copy
quicksort, return Array = self
if self.count > 1
id x = self[self.count / 2]
lesser := self.filter: (id y | return y < x)
greater := self.filter: (id y | return y > x)
return lesser.quicksort + [x] + greater.quicksort
end
int main()
autoreleasepool
a := [1, 3, 5, 7, 9, 8, 6, 4, 2]
Log( 'Unsorted: %@', a)
Log( 'Sorted: %@', a.quicksort )
b := ['Emil', 'Peg', 'Helen', 'Juergen', 'David', 'Rick', 'Barb', 'Mike', 'Tom']
Log( 'Unsorted: %@', b)
Log( 'Sorted: %@', b.quicksort )
return 0