Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace example
- {
- class Program
- {
- public static void Main(string[] args)
- {
- int[] arr = { 12, 0, 5, 7, 6, -11, -2, -30, 8, 7 };
- ShowArray(arr);
- SortAbs(arr);
- ShowArray(arr);
- Console.ReadKey();
- }
- public static void ShowArray(int[] arr)
- {
- foreach (var a in arr) Console.Write(a + " ");
- Console.WriteLine();
- }
- public static void SortAbs(int[] arr)
- {
- int size = arr.Length;
- int last = LastElement(arr);
- int temp;
- int next;
- if (arr[size - 1] != last)
- {
- for (int k = 0; k < size; k++)
- {
- if (arr[k] == last)
- {
- temp = arr[size - 1];
- arr[size - 1] = arr[k];
- arr[k] = temp;
- break;
- }
- }
- }
- for (int n = size - 2; n >= 0; n--)
- {
- next = NextElement(arr, n);
- if (arr[n] != next)
- {
- for (int t = 0; t < size; t++)
- {
- if (arr[t] == next)
- {
- temp = arr[n];
- arr[n] = arr[t];
- arr[t] = temp;
- break;
- }
- }
- }
- }
- }
- public static int NextElement(int[] arr, int stopIndex)
- {
- int max = 0;
- int temp;
- int next = arr[stopIndex];
- for (int n = stopIndex; n >= 0; n--)
- {
- temp = Math.Abs(arr[n] - arr[stopIndex + 1]);
- if (temp > max)
- {
- max = temp;
- next = arr[n];
- }
- if (temp == Int32.MaxValue) break;
- }
- return next;
- }
- public static int LastElement(int[] arr)
- {
- int max = 0;
- int temp;
- int size = arr.Length;
- bool flag = false;
- int last = arr[size - 1];
- for (int n = 0; n < size; n++)
- {
- for (int m = 0; m < size; m++)
- {
- if (n == m) continue;
- temp = Math.Abs(arr[m] - arr[n]);
- if (temp > max)
- {
- max = temp;
- last = arr[m];
- }
- if (temp == Int32.MaxValue)
- {
- flag = true;
- break;
- }
- }
- if (flag) break;
- }
- return last;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement