0% found this document useful (0 votes)
8 views

PROGRAM 1

Dsa

Uploaded by

aysingh7598
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

PROGRAM 1

Dsa

Uploaded by

aysingh7598
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

PROGRAM 1

AIM: To implement Selection Sort in C


ALGORITHM:
PROGRAM CODE:
#include<stdio.h>
void selectionSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
int min_idx = i;

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


if (arr[j] < arr[min_idx]) {

// Update min_idx if a smaller element is found


min_idx = j;
}
}

// Move minimum element to its


// correct position
int temp = arr[i];
arr[i] = arr[min_idx];
arr[min_idx] = temp;
}
}

void printArray(int arr[], int n) {


for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}

int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr) / sizeof(arr[0]);

printf("Original array: ");


printArray(arr, n);

selectionSort(arr, n);

printf("Sorted array: ");


printArray(arr, n);

return 0;
}

Output
Original vector: 64 25 12 22 11
Sorted vector: 11 12 22 25 64
PROGRAM 2:
AIM: To implement bubble sort in C
ALGORITHM:
PROGRAM CODE:
#include <stdbool.h>
#include <stdio.h>

void swap(int* xp, int* yp){


int temp = *xp;
*xp = *yp;
*yp = temp;
}

// An optimized version of Bubble Sort


void bubbleSort(int arr[], int n){
int i, j;
bool swapped;
for (i = 0; i < n - 1; i++) {
swapped = false;
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
swap(&arr[j], &arr[j + 1]);
swapped = true;
}
}

// If no two elements were swapped by inner loop,


// then break
if (swapped == false)
break;
}
}

// Function to print an array


void printArray(int arr[], int size){
int i;
for (i = 0; i < size; i++)
printf("%d ", arr[i]);
}

int main(){
int arr[] = { 64, 34, 25, 12, 22, 11, 90 };
int n = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, n);
printf("Sorted array: \n");
printArray(arr, n);
return 0;
}

Output
Sorted array:
11 12 22 25 34 64 90
PROGRAM 3:
AIM: To implement insertion sort in C
ALGORITHM:
PROGRAM CODE:
// C program to implement insertion sort
#include <math.h>
#include <stdio.h>

void insertionSort(int arr[], int N) {

// Starting from the second element


for (int i = 1; i < N; i++) {
int key = arr[i];
int j = i - 1;

// Move elements of arr[0..i-1], that are


// greater than key, to one position to
// the right of their current position
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}

// Move the key to its correct position


arr[j + 1] = key;
}
}

int main() {
int arr[] = { 12, 11, 13, 5, 6 };
int N = sizeof(arr) / sizeof(arr[0]);

printf("Unsorted array: ");


for (int i = 0; i < N; i++) {
printf("%d ", arr[i]);
}
printf("\n");

// Calling insertion sort on array arr


insertionSort(arr, N);

printf("Sorted array: ");


for (int i = 0; i < N; i++) {
printf("%d ", arr[i]);
}
printf("\n");

return 0;
}

Output
Unsorted array: 12 11 13 5 6
Sorted array: 5 6
PROGRAM 4:
AIM: To implement Linked list in C
ALGORITHM:
PROGRAM CODE:
// C Program to create a Linked List
#include <stdio.h>
#include <stdlib.h>

// Define the structure of Node


typedef struct Node {

// Data field. Can add more data according to our need


int data;

// Pointer to the next node


struct Node *next;
} Node;

int main() {

// Create the First Node of the Linked List


// This will serve as the head of the list
Node *first = (Node *)malloc(sizeof(Node));

// Assigning data
first->data = 10;

// Creating the second node in the list


Node *second = (Node *)malloc(sizeof(Node));

// Assigning data
second->data = 20;

// Creating the third node


Node *third = (Node *)malloc(sizeof(Node));

// Assigning data
third->data = 30;

// Linking the nodes


first->next = second; // This will create: 10 -> 20
second->next = third; // This will create: 10 -> 20 -> 30
third->next = NULL; // This will create: 10 -> 20 -> 30 -> NULL

printf("Linked List: ");


Node* temp = first;
while(temp) {
printf("%d ", temp->data);
temp = temp->next;
}

return 0;
}
Output
Linked List: 10 20 30

You might also like