Renditja e vektoreve

 

Renditja e vektoreve sipas metodes bubble sort

 

Sipas kesaj metode ne hapin i (per i nga 0 ne n-1)  krahasohet elementi me indeks j (duke filluar nga 0) me elementin pasardhes j+1. Nese i pari eshte me i madh se i dyti ato kembehen. Perseritet ky veprim per j nga 0 ne n – i-1.

// Renditja bubble sort

#include <iostream>

#include <iomanip>

using namespace std;

void SortA (int[], int);

int main()

{

   const int arraySize=7;

   int ARRAY[arraySize] = {2,6,4,8,10,12,89};

  

   cout<<"Elementet para radhitjes:\n";

 

   for (int i=0; i<arraySize; i++)

      cout<<setw(4)<<ARRAY[i];

      cout<<endl;

   cout<<"Elementet e radhitura ne kah zbrites:\n";

   SortA (ARRAY, arraySize);

system("pause");

   return 0;

}

void SortA (int a[], int size)

{   int hold=0;

    for (int pass=0; pass<size-1;pass++)

        for (int i = 0; i<size-pass-1; i++)

            if (a[i]<a[i+1])

            {

                hold=a[i];

                a[i]=a[i+1];

                a[i+1]=hold;

            }

           

            for (int i=0;i<size;i++)

      cout<<setw(4)<<a[i];

      cout<<endl;

}

2. Renditja "exchange sort"

Sipas Kesaj metode krahasohet cdo element i vektorit per (i nga 0 ne n-2) me te gjithe elementet e tjere pasardhes (per j nga i+1 deri n-1). Nese elementi me indeks i eshte me i madh elementi me indeks j, ata kembehen.

 

#include <iostream>

#include <iomanip>

 

using namespace std;

 

int main()

{

   const int size=7;

   int a[size] = {12,6,114,8,10,12,89};

   int i,j;

   cout<<"Elementet para radhitjes:\n";

 

   for (i=0; i<size; i++)

      cout<<setw(4)<<a[i];

      cout<<endl;

 

    int temp;

   

      for ( i=0; i<size-1; i++)

       for (j = i+1; j<size; j++)

           if (a[i]>a[j])

           {

               temp=a[i];

               a[i]=a[j];

               a[j]=temp;

           }

   cout<<"Elementet e radhitura ne kah rrites:\n";

  for ( i=0; i<size; i++)

      cout<<setw(4)<<a[i];

      cout<<endl;

system ("pause");

   return 0;

}

/* printon:

Elementet para radhitjes:

  12   6 114   8  10  12  89

Elementet e radhitura ne kah rrites:

   6   8  10  12  12  89 114

Press any key to continue . . .

*/

3. Insertion Sort.

Sipas kesaj metode ne hapin e pare krahasohet elementi i dyte me te parin. Nese eshte me i vogel ata kembehen. Ne hapin e dyte kapet elementi i trete dhe vendoset ne pozicionin e duhur te nen-vektorit paraardhes (qe perbehet nga elementi i pare dhe i dyte) Ne hapin e i-te, nenvektori me i elementet e pare te vektorit origjinal do te jete i renditur.

// Renditja Insertion Sort

#include <iostream>

using std::cout;

using std::endl;

 

#include <iomanip>

using std::setw;

int main()

{

   const int arraySize = 10; // size of array a

   int data[ arraySize ] = { 34, 56, 4, 10, 77, 51, 93, 30, 5, 52 };

   int insert; // temporary variable to hold element to insert

   cout << "Unsorted array:\n";

   // output original array

   for ( int i = 0; i < arraySize; i++ )

      cout << setw( 4 ) << data[ i ];

 

   // insertion sort

   // loop over the elements of the array

   for ( int next = 1; next < arraySize; next++ )

   {

      insert = data[ next ]; // store the value in the current element

            int moveItem = next; // initialize location to place element

       // search for the location in which to put the current element    

      while ( ( moveItem > 0 ) && ( data[ moveItem - 1 ] > insert ) )

      {

         // shift element one slot to the right

         data[ moveItem ] = data[ moveItem - 1 ];

         moveItem--;

      } // end while

  

      data[ moveItem ] = insert; // place inserted element into the array

   } // end for

 

   cout << "\nSorted array:\n";

 

   // output sorted array

   for ( int i = 0; i < arraySize; i++ )

      cout << setw( 4 ) << data[ i ];

   cout << endl;

   return 0; // indicates successful termination

} // end main