Manual Array Sorting
Sorting is a fundamental operation in programming because it helps you organize data to make searching, analyzing, and presenting information more efficient. While C# provides built-in methods to sort arrays, understanding how sorting algorithms work gives you valuable insight into algorithmic thinking and problem solving. Manual sorting algorithms, such as selection sort, allow you to see how elements are compared and moved, step by step, to achieve a sorted array. This understanding is crucial for situations where you need custom sorting logic or are working in environments with limited library support.
Selection sort is a simple sorting algorithm that repeatedly selects the smallest (for ascending order) or largest (for descending order) element from the unsorted portion of the array and moves it to its correct position in the sorted portion.
Program.cs
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849using System; namespace ConsoleApp { public class Program { public static void Main(string[] args) { int[] numbers = { 5, 2, 9, 1, 5, 6 }; Console.WriteLine("Original array:"); PrintArray(numbers); SelectionSortAscending(numbers); Console.WriteLine("Sorted array (ascending):"); PrintArray(numbers); } public static void SelectionSortAscending(int[] array) { int n = array.Length; for (int i = 0; i < n - 1; i++) { int minIndex = i; for (int j = i + 1; j < n; j++) { if (array[j] < array[minIndex]) { minIndex = j; } } int temp = array[i]; array[i] = array[minIndex]; array[minIndex] = temp; } } public static void PrintArray(int[] array) { foreach (int num in array) { Console.Write(num + " "); } Console.WriteLine(); } } }
Selection sort works by dividing the array into a sorted and an unsorted part. At each step, it selects the smallest element from the unsorted portion and swaps it with the first unsorted element, thereby growing the sorted portion by one.
In the code above, you start by looping through each element in the array except the last one. For each position i, you look for the smallest value in the rest of the array (from i + 1 to the end). When you find a smaller element, you update minIndex. After the inner loop, you swap the element at position i with the element at minIndex, ensuring the smallest value is placed at the current position. This process repeats until the entire array is sorted in ascending order.
Program.cs
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849using System; namespace ConsoleApp { public class Program { public static void Main(string[] args) { int[] numbers = { 3, 8, 4, 7, 2, 9 }; Console.WriteLine("Original array:"); PrintArray(numbers); SelectionSortDescending(numbers); Console.WriteLine("Sorted array (descending):"); PrintArray(numbers); } public static void SelectionSortDescending(int[] array) { int n = array.Length; for (int i = 0; i < n - 1; i++) { int maxIndex = i; for (int j = i + 1; j < n; j++) { if (array[j] > array[maxIndex]) { maxIndex = j; } } int temp = array[i]; array[i] = array[maxIndex]; array[maxIndex] = temp; } } public static void PrintArray(int[] array) { foreach (int num in array) { Console.Write(num + " "); } Console.WriteLine(); } } }
The code defines a Program class with a Main method that demonstrates how to sort an integer array in descending order using selection sort. You start by initializing the numbers array and printing its original contents. The SelectionSortDescending method is then called to sort the array from largest to smallest. This method works by repeatedly searching for the maximum value in the unsorted part of the array and swapping it with the first unsorted element. After sorting, the program prints the updated array, showing the elements arranged in descending order.
1. What is the time complexity of selection sort?
2. How does selection sort differ from bubble sort?
3. Why might you implement sorting manually instead of using built-in methods?
¡Gracias por tus comentarios!
Pregunte a AI
Pregunte a AI
Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla
Awesome!
Completion rate improved to 3.57
Manual Array Sorting
Desliza para mostrar el menú
Sorting is a fundamental operation in programming because it helps you organize data to make searching, analyzing, and presenting information more efficient. While C# provides built-in methods to sort arrays, understanding how sorting algorithms work gives you valuable insight into algorithmic thinking and problem solving. Manual sorting algorithms, such as selection sort, allow you to see how elements are compared and moved, step by step, to achieve a sorted array. This understanding is crucial for situations where you need custom sorting logic or are working in environments with limited library support.
Selection sort is a simple sorting algorithm that repeatedly selects the smallest (for ascending order) or largest (for descending order) element from the unsorted portion of the array and moves it to its correct position in the sorted portion.
Program.cs
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849using System; namespace ConsoleApp { public class Program { public static void Main(string[] args) { int[] numbers = { 5, 2, 9, 1, 5, 6 }; Console.WriteLine("Original array:"); PrintArray(numbers); SelectionSortAscending(numbers); Console.WriteLine("Sorted array (ascending):"); PrintArray(numbers); } public static void SelectionSortAscending(int[] array) { int n = array.Length; for (int i = 0; i < n - 1; i++) { int minIndex = i; for (int j = i + 1; j < n; j++) { if (array[j] < array[minIndex]) { minIndex = j; } } int temp = array[i]; array[i] = array[minIndex]; array[minIndex] = temp; } } public static void PrintArray(int[] array) { foreach (int num in array) { Console.Write(num + " "); } Console.WriteLine(); } } }
Selection sort works by dividing the array into a sorted and an unsorted part. At each step, it selects the smallest element from the unsorted portion and swaps it with the first unsorted element, thereby growing the sorted portion by one.
In the code above, you start by looping through each element in the array except the last one. For each position i, you look for the smallest value in the rest of the array (from i + 1 to the end). When you find a smaller element, you update minIndex. After the inner loop, you swap the element at position i with the element at minIndex, ensuring the smallest value is placed at the current position. This process repeats until the entire array is sorted in ascending order.
Program.cs
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849using System; namespace ConsoleApp { public class Program { public static void Main(string[] args) { int[] numbers = { 3, 8, 4, 7, 2, 9 }; Console.WriteLine("Original array:"); PrintArray(numbers); SelectionSortDescending(numbers); Console.WriteLine("Sorted array (descending):"); PrintArray(numbers); } public static void SelectionSortDescending(int[] array) { int n = array.Length; for (int i = 0; i < n - 1; i++) { int maxIndex = i; for (int j = i + 1; j < n; j++) { if (array[j] > array[maxIndex]) { maxIndex = j; } } int temp = array[i]; array[i] = array[maxIndex]; array[maxIndex] = temp; } } public static void PrintArray(int[] array) { foreach (int num in array) { Console.Write(num + " "); } Console.WriteLine(); } } }
The code defines a Program class with a Main method that demonstrates how to sort an integer array in descending order using selection sort. You start by initializing the numbers array and printing its original contents. The SelectionSortDescending method is then called to sort the array from largest to smallest. This method works by repeatedly searching for the maximum value in the unsorted part of the array and swapping it with the first unsorted element. After sorting, the program prints the updated array, showing the elements arranged in descending order.
1. What is the time complexity of selection sort?
2. How does selection sort differ from bubble sort?
3. Why might you implement sorting manually instead of using built-in methods?
¡Gracias por tus comentarios!