Parallel Sort two arrays - C Data Structure

C examples for Data Structure:Sort

Description

Parallel Sort two arrays

Demo Code

#include <stdio.h>
#include <string.h>

#define MaxNameSize 14/*w  ww  .j a  va2 s  . c  o m*/

#define MaxNameBuffer MaxNameSize+1

#define MaxNames 8

void parallelSort(int lo, int hi, int max, char list[MaxNames][MaxNameBuffer], int id[]) {
  char key[MaxNameSize];
  for (int i = lo + 1; i <= hi; i++) {
    strcpy(key, list[i]);
    int m = id[i];
    int k = i - 1;
    while (k >= lo && strcmp(key, list[k]) < 0) {
      strcpy(list[k + 1], list[k]);
      id[k + 1] = id[k];
      --k;
    }
    strcpy(list[k + 1], key);
    id[k + 1] = m;
  }
}

int main() {
  char name[MaxNames][MaxNameBuffer] = { "T", "D","R", "S", "A", "S", "K", "O" };

  int id[MaxNames] = { 3,2,4,7,6,5,9,1 };

  parallelSort(0, MaxNames - 1, MaxNameBuffer, name, id);

  for (int i = 0; i < MaxNames; i++)
    printf("%-18s %d\n", name[i], id[i]);
}

Related Tutorials