You are on page 1of 4

NFC Institute of Engineering&Fertilizer

Research Faisalabad

Department of Electrical Engineering


Fourth Semester
Course Title:
Data Structure & Algorithms (EE-232)
Topic:
Lab Assignments
Submitted To:
Dr. Salman Arain
Submitted By:
Hafeez Ali
Roll.No:
18-ELE-43
Reg.#:
2018-UET-NFC-FD-ELECT-43
Lab.No.5
SORTING AND ITS TECHNIQUES
Task#1
Write a function to create an array.
C Function:
int createArray( arraySize )
{
int n[ arraySize ];
int i;

printf("Enter %d integers: ", arraySize);

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


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

Task#2
Write a function to display an array.
C Function:
void printArray(int* array, int arraySize)
{
int i;

printf("Array: ");

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


printf("%d ", array[i]);
}
}
Task#3
Write a function for bubble sort of an array.
C Function:
void bubble_sort(int *array, int n)
{
int i, j, temp;

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


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

Task#4
Write a function for selection sort of an array.
C Function:
void selection_sort(int *array, int n)
{
int i, j, pos, swap;

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


pos = i;

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


if ( array[pos] > array[j] )
pos = j;
}
if ( pos != i ) {
swap = array[i];
array[i] = array[pos];
array[pos] = swap;
}
}
}

Task#5
Write a function for insertion sort of an array.
C Function:
void insertion_sort(int *array, int n)
{
int i, j, temp;

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


j = i;

while (j > 0 && array[j] < array[j-1]) {


temp = array[j];
array[j] = array[j-1];
array[j-1] = temp;
j = j-1;
}
}
}

Conclusion:
In this lab, I came to know the different sorting techniques and their use in
different arrays. The sorting techniques are as follows: Bubble, Insertion, Selection sort.

You might also like