Periyasamy Vellingirisamy
Write a program for sorting an array of integers using c#

Program for sorting an array of integers without using build in functions.

By Periyasamy Vellingirisamy in C# on Jun 01 2020
  • Ashok Kumawat
    Dec, 2021 29

    Sort an array using single loop.

    1. public static int[] SortArrayWithSingleLoop(int[] arr)
    2. {
    3. int length = arr.Length;
    4. for (int j = 0; j < length - 1; j++)
    5. {
    6. if (arr[j] > arr[j + 1])
    7. {
    8. int temp = arr[j];
    9. arr[j] = arr[j + 1];
    10. arr[j + 1] = temp;
    11. //reset index
    12. j = -1;
    13. }
    14. }
    15. return arr;
    16. }

    • 0
  • Vara Prasad
    Nov, 2021 8

    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();

    • 0
  • Periyasamy Vellingirisamy
    Jul, 2020 3

    Hi
    Refer following program.

    1. int[] numberArray = { 1, 3, 5, 67, 22, 90, 0, 6 };
    2. for (int i = 0; i < numberArray.Length; i++)
    3. {
    4. for (int j = i; j < numberArray.Length; j++)
    5. {
    6. if (numberArray[i] < numberArray[j])
    7. {
    8. int temp = numberArray[j];
    9. numberArray[j] = numberArray[i];
    10. numberArray[i] = temp;
    11. }
    12. }
    13. }
    14. for (int i = 0; i < numberArray.Length; i++)
    15. {
    16. Console.WriteLine(numberArray[i]);
    17. }
    18. Console.Read();

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS