[go: up one dir, main page]

0% found this document useful (0 votes)
6 views3 pages

Soooooorting

The document contains various sorting algorithms implemented in C, including Bubble Sort, Selection Sort, Insertion Sort, Merge Sort, and Quick Sort, along with a Priority Queue structure. Each sorting algorithm is briefly described with its functionality and method of operation. Additionally, the document outlines the implementation of a Priority Queue with functions for enqueueing, dequeueing, and displaying elements.
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)
6 views3 pages

Soooooorting

The document contains various sorting algorithms implemented in C, including Bubble Sort, Selection Sort, Insertion Sort, Merge Sort, and Quick Sort, along with a Priority Queue structure. Each sorting algorithm is briefly described with its functionality and method of operation. Additionally, the document outlines the implementation of a Priority Queue with functions for enqueueing, dequeueing, and displaying elements.
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/ 3

#include <stdio.

h> return 0;}


void bubbleSort(int arr[], int n) { #include <stdio.h>
for (int i = 0; i < n-1; i++) { void quickSort(int arr[], int low, int high) {
for (int j = 0; j < n-i-1; j++) { if (low < high) {
if (arr[j] > arr[j+1]) { int pivot = arr[high], i = low - 1;
int temp = arr[j]; for (int j = low; j <= high - 1; j++) {
arr[j] = arr[j+1]; if (arr[j] < pivot) {
arr[j+1] = temp;}}} i++;
void printArray(int arr[], int n) { int temp = arr[i];
for (int i = 0; i < n; i++) printf("%d ", arr[i] = arr[j];
arr[i]); arr[j] = temp; } }
printf("\n");} int temp = arr[i + 1];
int main() { arr[i + 1] = arr[high];
int arr[] = {64, 34, 25, 12, 22, 11, 90}; arr[high] = temp;
int n = sizeof(arr) / sizeof(arr[0]); int pi = i + 1;
bubbleSort(arr, n); quickSort(arr, low, pi - 1);
printArray(arr, n); quickSort(arr, pi + 1, high);}}
return 0;} void printArray(int arr[], int n) {
#include <stdio.h> for (int i = 0; i < n; i++) printf("%d ",
void insertionSort(int arr[], int n) { arr[i]);
for (int i = 1; i < n; i++) { printf("\n");}
int key = arr[i], j = i - 1; int main() {
while (j >= 0 && arr[j] > key) { int arr[] = {64, 34, 25, 12, 22, 11, 90};
arr[j + 1] = arr[j]; int n = sizeof(arr) / sizeof(arr[0]);
j--; } quickSort(arr, 0, n - 1);
arr[j + 1] = key; }} printArray(arr, n);
void printArray(int arr[], int n) { return 0;}
for (int i = 0; i < n; i++) printf("%d ", #include <stdio.h>
arr[i]); void merge(int arr[], int left, int right) {
printf("\n");} if (left < right) {
int main() { int middle = left + (right - left) / 2;
int arr[] = {64, 34, 25, 12, 22, 11, 90}; merge(arr, left, middle);
int n = sizeof(arr) / sizeof(arr[0]); merge(arr, middle + 1, right);
insertionSort(arr, n); int n1 = middle - left + 1, n2 = right -
printArray(arr, n); middle;
return 0;} int L[n1], R[n2];
#include <stdio.h> for (int i = 0; i < n1; i++) L[i] = arr[left
void selectionSort(int arr[], int n) { + i];
for (int i = 0; i < n-1; i++) { for (int j = 0; j < n2; j++) R[j] =
int minIndex = i; arr[middle + 1 + j];
for (int j = i + 1; j < n; j++) { int i = 0, j = 0, k = left;
if (arr[j] < arr[minIndex]) minIndex while (i < n1 && j < n2) {
= j; } if (L[i] <= R[j]) arr[k++] = L[i++];
int temp = arr[minIndex]; else arr[k++] = R[j++]; }
arr[minIndex] = arr[i]; while (i < n1) arr[k++] = L[i++];
arr[i] = temp; }} while (j < n2) arr[k++] = R[j++];}}
void printArray(int arr[], int n) { void printArray(int arr[], int n) {
for (int i = 0; i < n; i++) printf("%d ", for (int i = 0; i < n; i++) printf("%d ",
arr[i]); arr[i]);
printf("\n");} printf("\n");}
int main() { int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90}; int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]); int n = sizeof(arr) / sizeof(arr[0]);
selectionSort(arr, n); merge(arr, 0, n - 1);
printArray(arr, n); printArray(arr, n);
return 0;} priority queu void freeQueue(PriorityQueue* q) {
#include <stdio.h> free(q);}
int main() {
#include <stdlib.h>
PriorityQueue* pq = createQueue();
#define MAX_SIZE 5
enqueue(pq, 10);
typedef struct PriorityQueue {
enqueue(pq, 20);
int arr[MAX_SIZE]; int size;
enqueue(pq, 30);
} PriorityQueue;
enqueue(pq, 5);
PriorityQueue* createQueue() {
enqueue(pq, 50);
PriorityQueue* q =
display(pq);
(PriorityQueue*)malloc(sizeof(PriorityQueue
dequeue(pq);
));
display(pq);
q->size = 0;
printf("Front element: %d\n", front(pq));
return q;}
freeQueue(pq);
int isEmpty(PriorityQueue* q) {
return 0;}
return (q->size == 0);}
 Bubble Sort 🫧
int isFull(PriorityQueue* q) {
 Repeatedly compares and swaps
return (q->size == MAX_SIZE);}
adjacent elements until the array is
void enqueue(PriorityQueue* q, int value) {
sorted.
if (isFull(q)) {
 Selection Sort 🎯
printf("Priority Queue is full, cannot
 Finds the smallest element and
enqueue\n");
swaps it with the first, repeating the
return; }
process.
int i = q->size - 1;
 Insertion Sort 📌
while (i >= 0 && q->arr[i] < value) {
 Inserts each element into its correct
q->arr[i + 1] = q->arr[i];
position within an already sorted
i--; }
part.
q->arr[i + 1] = value;
 Merge Sort 🔀
q->size++;
 Recursively splits the array into two
printf("%d enqueued to priority queue\
halves, sorts each, and merges
n", value);}
them.
void dequeue(PriorityQueue* q) {
 Quick Sort ⚡
if (isEmpty(q)) {
 Selects a pivot, partitions elements
printf("Priority Queue is empty, cannot
around it, and recursively sorts
dequeue\n");
subarrays.
return }
 Dfs  Start from a given node.
int dequeuedValue = q->arr[0];
  Visit an adjacent unvisited node,
for (int i = 0; i < q->size - 1; i++) {
then visit its adjacent nodes,
q->arr[i] = q->arr[i + 1]; }
continuing this process.
q->size--;
  If there are no more unvisited
printf("%d dequeued from priority queue\
adjacent nodes, backtrack to the last
n", dequeuedValue);}
visited node with unvisited adjacent
int front(PriorityQueue* q) {
nodes.
if (isEmpty(q)) {
  Repeat until all nodes are visited.
printf("Priority Queue is empty\n");
 Bfs  Start from a given node.
return -1; }
 Visit all the adjacent nodes and
return q->arr[0];}
enqueue them.
void display(PriorityQueue* q) {
 Dequeue a node, visit its adjacent
if (isEmpty(q)) {
unvisited nodes, and enqueue those.
printf("Priority Queue is empty\n");
 Repeat until all nodes are visited.
return; }
 
printf("Priority Queue elements: ");
for (int i = 0; i < q->size; i++) {
printf("%d ", q->arr[i]);
}
printf("\n");}

You might also like