static void Swap(ref CElem x1, ref CElem x2) { CElem tmp = x1; x1 = x2; x2 = tmp; }
static void ShakerSort(CElem[] m, uint n) { uint k = n, r = n - 1; uint l = 1, j; do { for (j = r; j >= l; j--) if (m[j - 1].key > m[j].key) { Swap(ref m[j - 1], ref m[j]); k = j; } l = k + 1; for (j = l; j <= r; j++) if (m[j - 1].key > m[j].key) { Swap(ref m[j - 1], ref m[j]); k = j; } r = k - 1; } while (l <= r); }
static void shellSort(CElem[] m, uint n) { int i,j,k,s; uint stepInd; CElem x; for (stepInd = 0; stepInd < STEPS_CNT; stepInd++) { s = -(k = (int)steps[stepInd]); /* Ограничител */ for (i = k + 1; i <= (int)n; i++) { x = m[i]; j = i - k; if (0 == s) s = -k; m[++s] = x; while (x.key < m[j].key) { m[j + k] = m[j]; j -= k; } m[j + k] = x; } } }
/* Запълва масива със случайни цели числа */ static void init(CElem[] m, uint n) { uint i; Random rand = new Random(); for (i = 1; i <= n; i++) m[i].key = (int)(rand.Next() % n); }
/* Извежда ключовете на масива на екрана */ static void print(CElem[] m, uint n) { uint i; for (i = 1; i <= n; i++) Console.Write("{0}, ", m[i].key); Console.WriteLine(); }
static void Main(string[] args) { CElem[] m = new CElem[Max]; Init(m, Max); Console.WriteLine("Масивът преди сортирането:"); Print(m, Max); Console.WriteLine("Масивът след сортирането:"); SetSort(m, Max); Console.WriteLine(); }
static void Main(string[] args) { CElem[] m = new CElem[Max], saveM = new CElem[Max]; uint loopInd; Console.WriteLine("start -- "); for (loopInd = 1; loopInd <= TestLoopCnt; loopInd++) { Init(m, Max); m.CopyTo(saveM, 0); /* Запазва се копие на масива */ Console.WriteLine("Масивът преди сортирането:"); Print(m, Max); ShakerSort(m, Max); Console.WriteLine("Масивът след сортирането:"); Print(m, Max); Check(m, saveM, Max); } }
static void Main(string[] args) { CElem[] m = new CElem[MAX + 1], saveM = new CElem[MAX + 1]; uint loopInd; Console.WriteLine("start -- "); for (loopInd = 1; loopInd <= TEST_LOOP_CNT; loopInd++) { init(m, MAX); m.CopyTo(saveM, 0); Console.WriteLine("Масивът преди сортирането:"); print(m, MAX); shellSort(m, 1, MAX); Console.WriteLine("Масивът след сортирането:"); print(m, MAX); check(m, saveM, MAX); } }
static void check(CElem[] m, CElem[] saveM, uint n) { uint i, j; bool[] found = new bool[n + 1]; /* третира се като масив от булев тип */ /* 1. Проверка за наредба във възходящ ред */ for (i = 1; i < n; i++) if (m[i].key > m[i + 1].key) Environment.Exit(0); /* 2. Проверка за пермутация на изходните елементи */ for (i = 1; i <= n; i++) { for (j = 1; j <= n; j++) if (found[j] == false && m[i].key == saveM[j].key) { found[j] = true; break; } if (j > n) Environment.Exit(0); /* Пропада, ако не е намерен съответен */ } }
/* Сортира масив с използване на множество */ static void SetSort(CElem[] array, uint n) { int[] indexSet = new int[MaxValue]; /* Индексно множество */ /* 0. Инициализиране на множеството */ for (int i = 0; i < MaxValue; i++) indexSet[i] = NOIndex; /* 1. Формиране на множеството */ for (int j = 0; j < n; j++) { if ((array[j].Key >= 0 && array[j].Key < MaxValue) == false || (NOIndex == indexSet[array[j].Key]) == false) Environment.Exit(0); indexSet[array[j].Key] = j; } /* 2. Генериране на сортирана последователност */ for (int i = 0; i < MaxValue; i++) if (NOIndex != indexSet[i]) Do4Elem(array[indexSet[i]]); }
static void shellSort(CElem[] m, uint l, uint r) { uint[] incs = { 1391376, 463792, 198768, 86961, 33936, 13776, 4592, 1968, 861, 336, 112, 48, 21, 7, 3, 1 }; uint i, j, k, h; CElem v; for (k = 0; k < 16; k++) for (h = incs[k], i = l + h; i <= r; i++) { v = m[i]; j = i; while (j > h && m[j - h].key > v.key) { m[j] = m[j - h]; j -= h; } m[j] = v; } }
/* Запълва масива със случайни цели числа */ static void Init(CElem[] array, uint n) { /* 1. Запълване със случайни стойности в нарастващ ред */ Random rand = new Random(); array[0].Key = rand.Next() % Factor; for (int i = 1; i < n; i++) array[i].Key = 1 + array[i - 1].Key + rand.Next() % Factor; /* 2. Разменяме елементи */ for (int i = 1; i < n; i++) { long index1, index2; CElem tmp; /* 2.1. Избиране на два случайни индекса */ index1 = rand.Next() % n; index2 = rand.Next() % n; /* 2.2. Разменяме ги */ tmp = array[index1]; array[index1] = array[index2]; array[index2] = tmp; } }
static void Do4Elem(CElem e) { Console.Write("{0,4}", e.Key); }
/* Извежда ключовете на масива на екрана */ static void Print(CElem[] array, uint n) { for (int i = 0; i < n; i++) Console.Write("{0,4}", array[i].Key); Console.WriteLine(); }
/* Запълва масива със случайни цели числа */ static void Init(CElem[] m, uint n) { Random rand = new Random(); for (int i = 0; i < n; i++) m[i].key = (int)(rand.Next() % n); }
/* Извежда ключовете на масива на екрана */ static void Print(CElem[] m, uint n) { for (int i = 0; i < n; i++) Console.Write("{0,4}", m[i].key); Console.WriteLine(); }