// ************************************************************************** //
//                                                                            //
//    eses                   eses                                             //
//   eses                     eses                                            //
//  eses    eseses  esesese    eses   Embedded Systems Group                  //
//  ese    ese  ese ese         ese                                           //
//  ese    eseseses eseseses    ese   Department of Computer Science          //
//  eses   eses          ese   eses                                           //
//   eses   eseses  eseseses  eses    University of Kaiserslautern            //
//    eses                   eses                                             //
//                                                                            //
// ************************************************************************** //
// This file implements the well-known insertion sort procedure. The main idea
// is to sort prefixes x[0..i-1] of the array x[0..N-1] and then store x[i] in
// a separate variable y so that all entries x[j+1..i-1] can be moved one place
// to the right and y can be stored at x[j+1].
// ************************************************************************** //

thread InsertionMaxSort {
    [10]nat x;
    nat y,n,i,j,jmax;
    // first create a test array in reverse order
//     for(i=0..9) {
//         x[i] = 10-i;
//     }
    // now start sorting
    i = n-1;
    while(1 < i){
        // compute maximum of x[0..N-1-i]
        jmax = 0;
        j = 1;
        while(j <= i) {
        if(x[jmax] < x[j])
            jmax = j;
        j = j+1;
        }
        // now x[jmax] is the maximum of x[0..N-1-i] and is swapped with x[i]
        if(jmax != i) {
            y = x[i];
            x[i] = x[jmax];
            x[jmax] = y;
        }
        i = i-1;
    }
}