Author 
Message 
jutar
Joined: Apr 25, 2016 Posts: 2 Location: Connecticut

Posted: Mon Apr 25, 2016 7:18 pm Post subject:
Help with sort functions  Arrays and logic Subject description: Trying to write using randomly generated sets, stuck on the logic 


I'm working on a project using ChucK to generate random sets. The end goal is to use those sets to play a fuguestyle piece, with each successive variation and response as a manipulation of the set. I got ChucK to shuffle my tones, but I'm stuck sorting them into Normal form. Once I get this down, inversions will be easy, though Prime form will be its own headache. Here's what I have so far. I'm hitting two blocks. 1 the swap function overwrites items that shouldn't be in the swap. 2 I get an array out of bounds error when printing the results and I can't find the source. Here's my code so far:
Code: 
int i,j, dummy, iMin, hold;
[5, 3, 6] @=> int a[];
/* advance the position through the entire array */
/* (could do j < n1 because single element is also min element) */
for ( 0 => j; j <= a.cap()1; j++) {
/* assume the min is the first element */
j => iMin;
/* test against elements after j to find the smallest */
for ( j+1 => i; i < a.cap()1 ; i++) {
if(a[i] < a[iMin]){
i => iMin;
}
}
<<< a[i], i, a[iMin], iMin, a[j], j, "pre">>>;
if (iMin != j) {
a[j] => dummy;
a[iMin] => a[j];
<<<a[i], i, a[iMin], iMin, a[j], j, "mid">>>;
dummy => a[iMin];
}
<<< a[i], i, a[iMin], iMin, a[j], j, "post">>>;
}
for(int k; k < a.cap(); k++) <<< "Set1[", k,"] = ", a[k] >>>;
 [/code] 

Back to top



randbits
Joined: Jun 28, 2016 Posts: 6 Location: Utica, NY, USA

Posted: Tue Jun 28, 2016 6:22 pm Post subject:
Fixed Subject description: now the selection sort algorithm 


The righttoleft assignment of values can make things a bit confusing.
The code now sorts in ascending order correctly.
Code: 
int i,j, dummy, iMin;
[5, 10, 3, 6, 1, 9, 2, 7, 8] @=> int a[];
/* advance the position through the entire array */
for ( 0 => j; j < a.cap(); j++) {
/* assume the min is the first item */
j => iMin;
/* test against elements after j to find the smallest */
for ( j+1 => i; i < a.cap() ; i++) {
if(a[i] < a[iMin]) {
i => iMin;
}
}
a[j] => dummy;
a[iMin] => a[j];
dummy => a[iMin];
}
for(int k; k < a.cap(); k++) <<< "Set1[", k,"] = ", a[k] >>>;

This algorithm is very slow. If you feel like taking on a challenge, implement mergeSort instead (https://en.wikipedia.org/wiki/Merge_sort) Wicked fast!
Cheers 

Back to top



MusicMan11712
Joined: Aug 08, 2009 Posts: 1009 Location: Out scouting . . .

Posted: Wed Jun 29, 2016 12:22 pm Post subject:



Why would someone want to sort values like this as part of making a music generating program? If randomly generated numbers are assigned to an array in the order they are generated, it seems to me having an algorithm to sort the numbers into some other nonrandom order defeats the purpose of having randomly generated numbers in the first place. Just wondering.
Steve 

Back to top



Blue Hell
Site Admin
Joined: Apr 03, 2004 Posts: 22555 Location: The Netherlands, Enschede
Audio files: 220
G2 patch files: 319

Posted: Thu Jun 30, 2016 2:45 pm Post subject:



The idea to me seems to be to make sets of numbers. To do operations on sets more efficiently it makes sense to sort the elements (although the order of the elements in a set is, by definition, irrelevant).
Anyway, the numbers would still be random, and it would be very easy to extract them again in a random order by randomly indexing the elements.
Sorry for not being able to answer the ChucK question. _________________ Jan 

Back to top



