If you are a c# beginner or want to start learning the c# programming language, then this program will help you to understand the basics of c# programming. We are going to share C# program to sort an array using pancake sort.
Copy the below c# program and execute it in your Microsoft Visual Studio IDE (Integrated Development Environment ).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | using System; class pancakeSortProgram { static void flip(int []arr, int i) { int temp, start = 0; while (start < i) { temp = arr[start]; arr[start] = arr[i]; arr[i] = temp; start++; i--; } } static int findMax(int []arr, int n) { int mi, i; for (mi = 0, i = 0; i < n; ++i) if (arr[i] > arr[mi]) mi = i; return mi; } static int pancakeSort(int []arr, int n) { for (int curr_size = n; curr_size > 1; --curr_size) { int mi = findMax(arr, curr_size); if (mi != curr_size - 1) { flip(arr, mi); flip(arr, curr_size - 1); } } return 0; } static void printArray(int []arr, int arr_size) { for (int i = 0; i < arr_size; i++) Console.Write(arr[i] + " "); Console.Write(""); } public static void Main () { int []arr = {23, 10, 20, 11, 12, 6, 7}; int n = arr.Length; pancakeSort(arr, n); Console.Write("Sorted Array: "); printArray(arr, n); } } |
6 7 10 11 12 20 23
Liked this program? Do Like & share with your friends.
References: http://en.wikipedia.org/wiki/Pancake_sorting
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.