Bubble Sort

Properties

  • Stable
  • O(1) extra space
  • O(n2) comparisons and swaps
  • Adaptive: O(n) when nearly sorted

Discussion

Bubble sort has many of the same properties as insertion sort, but has slightly higher overhead. In the case of nearly sorted data, bubble sort takes O(n) time, but requires at least 2 passes through the data (whereas insertion sort requires something more like 1 pass).

    Program C Code

    //Bubble//
    void bubble (int * gelendizi, int dizi_boyut)
    {
        int temp,i,j;

        for (i=1; i<dizi_boyut; i++)
        {
            for (j=0; j<dizi_boyut-i; j++)
            {
                if(gelendizi[j] > gelendizi[j+1])
                {
                    temp = gelendizi [j];
                    gelendizi [j] = gelendizi [j+1];
                    gelendizi [j+1] = temp;
                }
            }
        }
    }

    Hiç yorum yok:

    Yorum Gönder