Program for sorting an array of integers without using build in functions.
Sort an array using single loop.
public static int[] SortArrayWithSingleLoop(int[] arr){ int length = arr.Length; for (int j = 0; j < length - 1; j++) { if (arr[j] > arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; //reset index j = -1; } } return arr;}
public static int[] SortArrayWithSingleLoop(int[] arr)
{
int length = arr.Length;
for (int j = 0; j < length - 1; j++)
if (arr[j] > arr[j + 1])
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
//reset index
j = -1;
}
return arr;
int[] A = { 10, 15, 9, 36, 28, 19, 34, 6 }; int temp; for (int i = 0; i < A.Length; i++) { for (int j = i+1; j < A.Length; j++) { if (A[i]>A[j]) { temp = A[i]; A[i] = A[j]; A[j] = temp; } } } for (int i = 0; i < A.Length; i++) { Console.Write(A[i] + “ “); } Console.ReadLine();
HiRefer following program.
int[] numberArray = { 1, 3, 5, 67, 22, 90, 0, 6 }; for (int i = 0; i < numberArray.Length; i++) { for (int j = i; j < numberArray.Length; j++) { if (numberArray[i] < numberArray[j]) { int temp = numberArray[j]; numberArray[j] = numberArray[i]; numberArray[i] = temp; } } } for (int i = 0; i < numberArray.Length; i++) { Console.WriteLine(numberArray[i]); } Console.Read();
int[] numberArray = { 1, 3, 5, 67, 22, 90, 0, 6 };
for (int i = 0; i < numberArray.Length; i++)
for (int j = i; j < numberArray.Length; j++)
if (numberArray[i] < numberArray[j])
int temp = numberArray[j];
numberArray[j] = numberArray[i];
numberArray[i] = temp;
Console.WriteLine(numberArray[i]);
Console.Read();