Unit 14 - V1
Unit 14 - V1
C PROGRAMMING
Unit 14
Function Part 4
Table of Contents
1. INTRODUCTION
Additionally, implementing sorting algorithms like bubble sort, selection sort, and insertion
sort using functions enhances code organization and readability. Functions encapsulate the
sorting logic, making it easier to understand and maintain. By separating the sorting
algorithms into functions, they can be reused across different parts of the program or in
other programs.
Overall, functions with arrays enhance code modularity, reusability, and readability in C
programming, making it easier to work with arrays and implement complex algorithms
efficiently. They play a crucial role in developing robust and maintainable software solutions.
1.1. Objectives:
Arrays can also be returned from functions. The function dynamically allocates
memory for the array and returns a pointer to the array.
Functions with arrays can implement algorithms such as sorting (e.g., bubble sort,
selection sort, insertion sort), searching, or other manipulations.
#include <stdio.h>
array[i] *= 2;
int main() {
printf("\n");
doubleArray(arr, arrSize);
printf("\n");
return 0;
This program defines a function doubleArray that takes an array and its size as arguments
and doubles each element of the array. In the main function, an array arr is declared and
initialized. The original array is printed, then passed to the doubleArray function to modify
its elements. Finally, the modified array is printed to the console.
Implement algorithms like bubble sort, selection sort, Insertion sort, using functions.
Sorting in C refers to arranging elements in a specified order, usually either ascending or
descending. There are various sorting algorithms available in C to accomplish this task, each
with its own advantages and disadvantages. Here are some common sorting algorithms
implemented in C:
Bubble Sort:
Bubble sort is a simple sorting algorithm that repeatedly steps through the list, compares
adjacent elements, and swaps them if they are in the wrong order.
Selection Sort:
Selection sort divides the input list into two parts: a sorted sublist and an unsorted sublist.
It repeatedly selects the minimum element from the unsorted part and swaps it with the first
element of the unsorted part.
Insertion Sort:
Insertion sort builds the final sorted list one item at a time by taking each element from the
unsorted part and inserting it into its correct position in the sorted part.
This C program demonstrates the implementation of three different sorting algorithms:
Bubble Sort, Selection Sort, and Insertion Sort.
#include <stdio.h>
// Bubble Sort
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
// Selection Sort
void selectionSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
// Swap arr[i] and arr[minIndex]
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}
// Insertion Sort
void insertionSort(int arr[], int n) {
int i, key, j;
for (i = 1; i < n; i++) {
key = arr[i];
j = i - 1;
// Move elements of arr[0..i-1], that are greater than key, to one position
ahead of their current position
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Original array: ");
printArray(arr, n);
// Bubble Sort
bubbleSort(arr, n);
printf("Array sorted using Bubble Sort: ");
printArray(arr, n);
// Selection Sort
int arr2[] = {64, 25, 12, 22, 11};
selectionSort(arr2, n);
printf("Array sorted using Selection Sort: ");
printArray(arr2, n);
// Insertion Sort
int arr3[] = {64, 25, 12, 22, 11};
insertionSort(arr3, n);
printf("Array sorted using Insertion Sort: ");
printArray(arr3, n);
return 0;
}
SELF-ASSESSMENT QUESTIONS - 1
1. Which syntax is used to pass an array to a function in C?
(a) void functionName(arrayType arrayName[], int arraySize)
(b) void functionName(arrayType *arrayName, int arraySize)
(c) void functionName(arrayType arrayName[], arraySize)
(d) void functionName(arraySize, arrayType arrayName[])
2. When passing an array to a function, which parameter is used to specify the size
of the array?
(a) arrayType
(b) arrayName
(c) arraySize
(d) functionType
3. How is an array returned from a function in C?
3. SUMMARY
Functions with arrays in C programming enable modular and reusable code by allowing
arrays to be passed to functions as arguments and returned from functions. This separation
of array manipulation logic from the main program enhances code organization and
readability. Additionally, implementing sorting algorithms like bubble sort, selection sort,
and insertion sort using functions further enhances code modularity and reusability. By
encapsulating the sorting logic within functions, it becomes easier to understand and
maintain, and the functions can be reused across different parts of the program or in other
programs. Overall, functions with arrays enhance code modularity, reusability, and
readability in C programming, making it easier to work with arrays and implement complex
algorithms efficiently, thereby contributing to the development of robust and maintainable
software solutions.
4. TERMINAL QUESTIONS