0% found this document useful (0 votes)
103 views9 pages

Programet Ne C Per Renditje Numrash

This document contains C code implementations of common sorting algorithms: selection sort, bubble sort, insertion sort, and merge sort. It includes the code for each algorithm, as well as driver code to test the sorting functions on sample data. The algorithms are selection sort, bubble sort, insertion sort, and merge sort.

Uploaded by

BiuShkreta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
103 views9 pages

Programet Ne C Per Renditje Numrash

This document contains C code implementations of common sorting algorithms: selection sort, bubble sort, insertion sort, and merge sort. It includes the code for each algorithm, as well as driver code to test the sorting functions on sample data. The algorithms are selection sort, bubble sort, insertion sort, and merge sort.

Uploaded by

BiuShkreta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Universiteti Politeknik i Tiranës

Fakulteti i Inxhinierisë Elektrike


Inxhinieri Mekatronike IIIB

Punë Laboratori
Lënda : Algoritëm dhe programim i
avancuar

Punoi : Vebi SHKRETA Pranoi : Dr. Marin ARANITASI


Programi në gjuhën C i Selection Sort
#include <stdio.h>

int main()

{
int a[100], n, j,i,temp,min;

printf("Fusni numrin e elementeve\n");

scanf("%d", &n);

printf("Fusni %d elemente\n", n);

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

scanf("%d", &a[i]);

for (i = 0; i < (n - 1); i++) {

min = i;

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

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

min = j;

if (min != i)

{
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}
printf("Renditja ne rend rrites eshte:\n");

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

printf("%d ", a[i]);

return 0;

}
Programi në gjuhën C i Bubble Sort
#include<stdio.h>
int main(){
int n,i,j,temp,a[20];
printf("Fusni numrin e elementeve : ");
scanf("%d",&n);
printf("Vendosni %d elemente: ",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n-1;i++){
for(j=i+1;j<n;j++){
if(a[i]>a[j]){
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("Pas renditjes: ");
for(i=0;i<n;i++)
printf(" %d",a[i]);

return 0;
}

Programi në gjuhën C i Insertion Sort


#include<stdio.h>
int main(){

int i,j,n,key,a[20];

printf("Fusni numrin total te elementeve: ");


scanf("%d",&n);
printf("Fusni %d elemente: ",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=1;i<n;i++){
key=a[i];
j=i-1;
while((key<a[j])&&(j>=0)){
a[j+1]=a[j];
j=j-1;
}
a[j+1]=key;
}
printf("Pas renditjes: ");
for(i=0;i<n;i++)
printf(" %d",a[i]);
return 0;
}
Programi në gjuhën C i Merge Sort
#include<stdlib.h>
#include<stdio.h>
// Merges two subarrays of arr[].
// First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
void merge(int arr[], int l, int m, int r)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
/* create temp arrays */
int L[n1], R[n2];
/* Copy data to temp arrays L[] and R[] */
for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1+ j];
/* Merge the temp arrays back into arr[l..r]*/
i = 0; // Initial index of first subarray
j = 0; // Initial index of second subarray
k = l; // Initial index of merged subarray
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
/* Copy the remaining elements of L[], if there
are any */
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
/* Copy the remaining elements of R[], if there
are any */
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
/* l is for left index and r is right index of the
sub-array of arr to be sorted */
void mergeSort(int arr[], int l, int r)
{
if (l < r)
{
// Same as (l+r)/2, but avoids overflow for
// large l and h
int m = l+(r-l)/2;
// Sort first and second halves
mergeSort(arr, l, m);
mergeSort(arr, m+1, r);

merge(arr, l, m, r);
}
}

/* UTILITY FUNCTIONS */
/* Function to print an array */
void printArray(int A[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", A[i]);
printf("\n");
}
/* Driver program to test above functions */
int main()
{
int arr[] = {12, 11, 13, 5, 6, 7};
int arr_size = sizeof(arr)/sizeof(arr[0]);

printf("Vektori i dhene \n");


printArray(arr, arr_size);

mergeSort(arr, 0, arr_size - 1);

printf("\n Vektori i renditur eshte \n");


printArray(arr, arr_size);
return 0;
}

You might also like