Answers for "c# bubble sort"

4

bubble sort c#

using System;

namespace BubbleSort {
   class Sorting {
      static void Main(string[] args) {
         int[] mixdata = { 56, 23, 2, 86, 45, 102 };
         int temp;

         for (int j = 0; j <= mixdata.Length - 2; j++) {
            for (int i = 0; i <= mixdata.Length - 2; i++) {
               if (mixdata[i] > mixdata[i + 1]) {
                  temp= mixdata[i + 1];
                  mixdata[i + 1] = mixdata[i];
                  mixdata[i] = temp;
               }
            }
         }

         Console.WriteLine("Bubble sort data:");
         foreach (int p in mixdata)
         Console.Write(p + " ");
         Console.Read();
      }
   }
}
Posted by: Guest on April-14-2020
4

bubble sort in java

public static void bubbleSort(int arr[])
{
	for (int i = 0; i < arr.length; i++) //number of passes
    {
		//keeps track of positions per pass      
    	for (int j = 0; j < (arr.length - 1 - i); j++) //Think you can add a -i to remove uneeded comparisons 
        {
          	//if left value is great than right value 
        	if (arr[j] > arr[j + 1])
            {
              	//swap values
            	int temp = arr[j];
              	arr[j] = arr[j + 1];
              	arr[j + 1] = temp; 
            }
        }
    }
}
Posted by: Guest on April-15-2020
0

bubble sort c#

using System;

namespace BubbleSort_3Languages
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] array = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
            bubblesort(array);

            foreach (int element in array)
            {
                Console.WriteLine(element);
            }
        }

        static void bubblesort(int[] array)
        {
            int len = array.Length;

            for (int i = 0; i < len; i++)
            {
                for (int j = 0; j < len; j++)
                {
                    int a = array[j];
                    if (a != array[len - 1])
                    {
                        int b = array[j + 1];
                        if (a > b)
                        {
                            array[j] = b;
                            array[j + 1] = a;
                        }
                    }
                }
            }
        }
    }
}
Posted by: Guest on May-29-2020
0

.net bubble sort

using System;

namespace BubbleSort_3Languages
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] array = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
            bubblesort(array);

            foreach (int element in array)
            {
                Console.WriteLine(element);
            }
        }

        static void bubblesort(int[] array)
        {
            int len = array.Length;

            for (int i = 0; i < len; i++)
            {
                for (int j = 0; j < len; j++)
                {
                    int a = array[j];
                    if (a != array[len - 1])
                    {
                        int b = array[j + 1];
                        if (a > b)
                        {
                            array[j] = b;
                            array[j + 1] = a;
                        }
                    }
                }
            }
        }
    }
}
using System;

namespace BubbleSort {
   class Sorting {
      static void Main(string[] args) {
         int[] mixdata = { 56, 23, 2, 86, 45, 102 };
         int temp;

         for (int j = 0; j <= mixdata.Length - 2; j++) {
            for (int i = 0; i <= mixdata.Length - 2; i++) {
               if (mixdata[i] > mixdata[i + 1]) {
                  temp= mixdata[i + 1];
                  mixdata[i + 1] = mixdata[i];
                  mixdata[i] = temp;
               }
            }
         }

         Console.WriteLine("Bubble sort data:");
         foreach (int p in mixdata)
         Console.Write(p + " ");
         Console.Read();
      }
   }
}
Posted by: Guest on August-17-2020

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language