Нужно найти элементы массива,значения которых больше последнего элемента и поместить их в новый массив - C#
Формулировка задачи:
Потом этот массив необходимо отсортировать методом Хоара. Подскажите пожалуйста,как правильно заполнить новый массив элементами. Видимо,я делаю что-то не так
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Diagnostics;
- using System.Threading;
- namespace CSharpSort
- {
- class Program
- {
- static public int Partition(int[] numbers, int left, int right)
- {
- int baz = numbers[left];
- while (true)
- {
- while (numbers[left] < baz)
- left++;
- while (numbers[right] > baz)
- right--;
- if (left < right)
- {
- int temp = numbers[right];
- numbers[right] = numbers[left];
- numbers[left] = temp;
- }
- else
- {
- return right;
- }
- }
- }
- static public void QuickSort_Recursive(int[] arr, int left, int right)
- {
- if (left < right)
- {
- int baz = Partition(arr, left, right);
- if (baz > 1)
- QuickSort_Recursive(arr, left, baz - 1);
- if (baz + 1 < right)
- QuickSort_Recursive(arr, baz + 1, right);
- }
- }
- static void Main(string[] args)
- {
- Stopwatch stopWatch = new Stopwatch();
- stopWatch.Start();
- int[] X = { 3, 8, 7, 5, 2, 1, 9, 9, 4 };
- // добавление в новый массив
- int k = 0;
- int[] nov = new int[9];
- int N = 9;
- for (int i = 0; i < N; i++)
- {
- if (X[i] > X[N - 1])
- {
- nov[k] = X[i];
- }
- }
- for (int i = 0; i < 9; i++) {
- Console.WriteLine(nov[i]); }
- Console.WriteLine("Sortirovka hoara ");
- QuickSort_Recursive(nov, 0, nov.Length - 1);
- for (int i = 0; i < 9; i++)
- Console.WriteLine(nov[i]);
- stopWatch.Stop();
- TimeSpan ts = stopWatch.Elapsed;
- string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
- ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds );
- Console.WriteLine(" "+elapsedTime);
- }
- }
- }
Решение задачи: «Нужно найти элементы массива,значения которых больше последнего элемента и поместить их в новый массив»
textual
Листинг программы
- using System;
- namespace CSharpSort
- {
- class Program
- {
- static int Partition(int[] array, int start, int end)
- {
- int marker = start;
- for (int i = start; i <= end; i++)
- {
- if (array[i] <= array[end])
- {
- int temp = array[marker]; // swap
- array[marker] = array[i];
- array[i] = temp;
- marker += 1;
- }
- }
- return marker - 1;
- }
- static void Sort(int[] array, int start, int end)
- {
- if (start >= end)
- {
- return;
- }
- int pivot = Partition(array, start, end);
- Sort(array, start, pivot - 1);
- Sort(array, pivot + 1, end);
- }
- static public void Show(int[] arr)
- {
- for (int i = 0; i < arr.Length; i++)
- {
- Console.Write(arr[i] + " ");
- }
- Console.WriteLine();
- }
- static void Main(string[] args)
- {
- int[] arr = { 3, 8, 7, 5, 2, 1, 9, 9, 4 };
- // подсчет кол-ва элементов нового массива
- Show(arr);
- int count = 0;
- int lastEl = arr[arr.Length - 1];
- for (int i = 0; i < arr.Length; i++)
- {
- if (arr[i] > lastEl)
- count++;
- }
- int k = 0;
- int[] sortedArr = new int[count];
- for (int i = 0; i < arr.Length; i++)
- {
- if (arr[i] > lastEl)
- {
- sortedArr[k] = arr[i];
- k++;
- }
- }
- Console.WriteLine("Unsorted: ");
- Show(sortedArr);
- Console.WriteLine("Sortirovka hoara ");
- Sort(sortedArr, 0, sortedArr.Length - 1);
- Console.WriteLine("Sorted:");
- Show(sortedArr);
- }
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д