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

C Programming

The document discusses three different quicksort algorithms: 1. Using the last element as the pivot 2. Using the middle element as the pivot 3. Adding intermediate output to track the sorting progress The last part also introduces insertion sort as an alternative sorting algorithm.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

C Programming

The document discusses three different quicksort algorithms: 1. Using the last element as the pivot 2. Using the middle element as the pivot 3. Adding intermediate output to track the sorting progress The last part also introduces insertion sort as an alternative sorting algorithm.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

}

SORTING }

Quick sort if (flag!=1) {


#include <stdio.h> while (a[loc]>=a[left] && (loc!=left))
int par(int a[],int beg,int end); left++;
int qs(int a[],int beg,int end); if (loc==left)
void main() flag=1;
{ else (a[loc]<a[left]); {
int a[]= {1,33,5,2,7,54,84,88,53}; int temp=a[loc];
int n=8; a[loc]=a[left];
qs(a,0,n-1); a[left]=temp;
for (int i=0; i<n; i++) { loc=left;
printf("%d ",a[i]); }

}
} return loc;
} }
int par(int a[],int beg,int end)

int left,right,loc; int qs(int a[],int beg,int end)


left=loc=beg; {
right=end; int loc;
int flag=0; if(beg<=end) {
while(flag!=1) { loc=par(a,beg,end);
while (a[loc]<=a[right] && (loc!=right)) qs(a,beg,loc-1);
{ right--;} qs(a,loc+1,end);
if (loc==right) }
{ flag=1;} }
else (a[loc]>a[right]) ;

int temp=a[loc];
Quick sort with last element as pivot
a[loc]=a[right]; #include <stdio.h>

a[right]=temp;

loc=right; int par(int a[], int beg, int end);

void qs(int a[], int beg, int end);


}

int main() {

int a[] = {1, 33, 5, 2, 7, 54, 84,90,2, 88, 53}; // Swap the pivot element with the element at
index left
int n = 10;
int temp = a[left];
qs(a, 0, n - 1);
a[left] = a[end];
for (int i = 0; i < n; i++) {
a[end] = temp;
printf("%d ", a[i]);

}
return left;
return 0;
}
}

void qs(int a[], int beg, int end) {


int par(int a[], int beg, int end) {
if (beg < end) {
int pivot = a[end]; // Use the last element as the
pivot int loc = par(a, beg, end);

int left = beg; qs(a, beg, loc - 1);

int right = end - 1; qs(a, loc + 1, end);

while (1) { }

while (left <= right && a[left] < pivot) {

left++; Quick sort with middle element as


} pivot
while (left <= right && a[right] > pivot) {

right--; #include <stdio.h>


}

if (left <= right) { int par(int a[], int beg, int end);
// Swap a[left] and a[right] void qs(int a[], int beg, int end);
int temp = a[left];

a[left] = a[right]; int main() {


a[right] = temp; int a[] = {1, 33, 5, 2, 7, 54, 84, 88, 53};
left++; int n = 9; // Note that I increased the size of the
right--; array to 9

} else { qs(a, 0, n - 1);

break; for (int i = 0; i < n; i++) {

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

}
return 0; }

void qs(int a[], int beg, int end) {

int par(int a[], int beg, int end) { if (beg < end) {

int pivot = a[end]; // Use the last element as the // Find the middle element and swap it with the
pivot last element to use as the pivot

int left = beg; int middle = (beg + end) / 2;

int right = end - 1; int temp = a[middle];

a[middle] = a[end];

while (1) { a[end] = temp;

while (left <= right && a[left] < pivot) {

left++; int loc = par(a, beg, end);

} qs(a, beg, loc - 1);

while (left <= right && a[right] > pivot) { qs(a, loc + 1, end);

right--; }

} }

if (left <= right) { Quick sort with intermediate


// Swap a[left] and a[right] outputs
int temp = a[left];

a[left] = a[right]; #include <stdio.h>


a[right] = temp;

left++; int par(int a[], int beg, int end);


right--; void qs(int a[], int beg, int end);
} else {

break; void printArray(int a[], int n) {


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

}
// Swap the pivot element with the element at printf("\n");
index left
}
int temp = a[left];

a[left] = a[end];
int main() {
a[end] = temp;
int a[] = {1, 33, 5, 2, 7, 54, 84, 88, 53};

int n = 9; // It should be 9 instead of 8 to match the


return left; array size.
qs(a, 0, n - 1); a[loc] = a[left];

printf("Sorted Array: "); a[left] = temp;

printArray(a, n); loc = left;

return 0; }

} }

return loc;

int par(int a[], int beg, int end) { }

int left, right, loc;

left = loc = beg; void qs(int a[], int beg, int end) {

right = end; int loc;

int flag = 0; if (beg < end) {

loc = par(a, beg, end);

while (flag != 1) { printf("Intermediate Array: ");

while (a[loc] <= a[right] && (loc != right)) { printArray(a, end + 1); // Print the array after
partitioning
right--;
qs(a, beg, loc - 1);
}
qs(a, loc + 1, end);
if (loc == right) {
}
flag = 1;
}
} else {

int temp = a[loc];

a[loc] = a[right]; Insertion sort


a[right] = temp;

loc = right; #include<stdio.h>

} int is(int a[],int n);

} int main()

if (flag != 1) { {

while (a[loc] >= a[left] && (loc != left)) { int a[]= {15,2,7,54,84,88,53,9};

left++; int n=8;

} for (int i=0; i<n; i++) {

if (loc == left) { printf("%d ",a[i]);}

flag = 1; printf("\n");

} else { is(a,n);

int temp = a[loc]; for (int i=0; i<n; i++) {


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

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

} printf("%d ", arr[i]);

} }

insertionSort(arr, n);

int is(int a[],int n)

{ printf("\nSorted Array: ");

int i,j,key; for (int i = 0; i < n; i++) {

for (i=1;i<n;i++) printf("%d ", arr[i]);

{ }

key=a[i];

j=i-1; return 0;

while(j>=0 && a[j]>key) }

a[j+1]=a[j]; void insertionSort(int arr[], int n) {

j=j-1; int i, key, j;

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

} key = arr[i];

a[j+1]=key; j = i - 1;

} while (j >= 0 && arr[j] > key) {

arr[j + 1] = arr[j];

Insertion sort with intermediate j = j - 1;

outputs }

#include <stdio.h> arr[j + 1] = key;

void insertionSort(int arr[], int n); printf("\nIntermediate Array: ");

for (int k = 0; k < n; k++) {

int main() { printf("%d ", arr[k]);

int arr[] = {12, 11, 13, 5, 6}; }

int n = sizeof(arr) / sizeof(arr[0]); }

}
Merge sort with intermediate }

outputs }

#include <stdio.h> void merge_sort(int a[], int beg, int end) {

int mid;

void merge(int a[], int beg, int mid, int end) { if (beg < end) {

int i = beg, j = mid + 1, index = beg, k; mid = (beg + end) / 2;

int temp[10]; merge_sort(a, beg, mid);

while (i <= mid && j <= end) { merge_sort(a, mid + 1, end);

if (a[i] < a[j]) { printf("\nintermediate array: ");

temp[index] = a[i]; for (int i = 0; i < 9; i++) {

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

} else { }

temp[index] = a[j]; merge(a, beg, mid, end);

j++; }

} }

index++;

} int main() {

int a[] = {1, 33, 5, 2, 7, 54, 84, 88, 53};

while (i <= mid) { int n = 9;

temp[index] = a[i]; merge_sort(a, 0, n - 1);

i++; printf("\nsorted array : ");

index++; for (int i = 0; i < n; i++) {

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

while (j <= end) {

temp[index] = a[j]; return 0;

j++; }

index++;

for (k = beg; k <= end; k++) {

a[k] = temp[k];
if (left < N && arr[left] > arr[largest])

Heap Sort largest = left;

#include <stdio.h> // If right child is larger than largest

// so far
// Function to swap the position of two elements if (right < N && arr[right] > arr[largest])

void swap(int* a, int* b) largest = right;


{

// Swap and continue heapifying


int temp = *a; // if root is not largest
*a = *b; // If largest is not root
*b = temp; if (largest != i) {
}

swap(&arr[i], &arr[largest]);
// To heapify a subtree rooted with node i

// which is an index in arr[]. // Recursively heapify the affected


// n is size of heap // sub-tree
void heapify(int arr[], int N, int i) heapify(arr, N, largest);
{ }
// Find largest among root, }
// left child and right child

// Main function to do heap sort


// Initialize largest as root void heapSort(int arr[], int N)
int largest = i; {

// left = 2*i + 1 // Build max heap


int left = 2 * i + 1; for (int i = N / 2 - 1; i >= 0; i--)

// right = 2*i + 2 heapify(arr, N, i);


int right = 2 * i + 2;

// Heap sort
// If left child is larger than root for (int i = N - 1; i >= 0; i--) {
swap(&arr[0], &arr[i]);

// Heapify root element

// to get highest element at

// root again

heapify(arr, i, 0);

// A utility function to print array of size n

void printArray(int arr[], int N)

for (int i = 0; i < N; i++)

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

printf("\n");

// Driver's code

int main()

int arr[] = { 12, 11, 13, 5, 6, 7 };

int N = sizeof(arr) / sizeof(arr[0]);

// Function call

heapSort(arr, N);

printf("Sorted array is\n");

printArray(arr, N);

// This code is contributed by _i_plus_plus_.


return -1;

SEARCHING }

Binary search linear search


#include <stdio.h>
#include <stdio.h>

int binarySearch(int arr[], int left, int right, int x);


int linearSearch(int arr[], int size, int x);

int main() {
int main() {
int arr[] = {1, 2, 3, 44, 77, 81, 90};
int arr[] = {1, 44, 3, 2, 77, 81, 90};
int n = 7;
int n = 7;
int x = 9;
int x = 7;
int result = binarySearch(arr, 0, n - 1, x);
int result = linearSearch(arr, n, x);

if (result == -1)
if (result == -1)
printf("Element not found\n");
printf("Element not found\n");
else
else
printf("Element found at index %d\n", result);
printf("Element found at index %d\n", result);

return 0;
return 0;
}
}

int binarySearch(int arr[], int left, int right, int x) {


int linearSearch(int arr[], int size, int x) {
while (left <= right) {
for (int i = 0; i < size; i++) {
int mid = left + (right - left) / 2;
if (arr[i] == x) {

return i; // Return the index of the found


if (arr[mid] == x) element
return mid; }

}
if (arr[mid] < x)

left = mid + 1; return -1; // Return -1 if the element is not found


else }
right = mid - 1;

}
// Stack is empty when top is equal to -1

int isEmpty(struct Stack* stack)


Array implementation of
{
stack return stack->top == -1;
#include <limits.h>
}
#include <stdio.h>

#include <stdlib.h>
// Function to add an item to stack. It increases top by
1

// A structure to represent a stack void push(struct Stack* stack, int item)

struct Stack { {

int top; if (isFull(stack))

unsigned capacity; return;

int* array; stack->array[++stack->top] = item;

}; printf("%d pushed to stack\n", item);

// function to create a stack of given capacity. It


initializes size of
// Function to remove an item from stack. It decreases
// stack as 0 top by 1

struct Stack* createStack(unsigned capacity) int pop(struct Stack* stack)

{ {

struct Stack* stack = (struct if (isEmpty(stack))


Stack*)malloc(sizeof(struct Stack));
return INT_MIN;
stack->capacity = capacity;
return stack->array[stack->top--];
stack->top = -1;
}
stack->array = (int*)malloc(stack->capacity *
sizeof(int));
// Function to return the top from stack without
return stack;
removing it
}
int peek(struct Stack* stack)

{
// Stack is full when top is equal to the last index
if (isEmpty(stack))
int isFull(struct Stack* stack)
return INT_MIN;
{
return stack->array[stack->top];
return stack->top == stack->capacity - 1;
}
}

// Driver program to test above functions


int main()

struct Stack* stack = createStack(100);

push(stack, 10);

push(stack, 20);

push(stack, 30);

printf("%d popped from stack\n", pop(stack));

return 0;

}
C program to illustrate
// copying structure using assignment
the use of structures operator
var2 = var1;
#include <stdio.h>

printf("Struct 1:\n\ti = %d, c = %c, f = %f, s


// declaring structure with name str1
= %s\n",
struct str1 {
var1.i, var1.c, var1.f, var1.s);
int i;
printf("Struct 2:\n\ti = %d, c = %c, f = %f, s
char c; = %s\n",
float f; var2.i, var2.c, var2.f, var2.s);
char s[30]; printf("Struct 3\n\ti = %d, c = %c, f = %f\
n", var3.ii,
};
var3.cc, var3.ff);

// declaring structure with name str2


return 0;
struct str2 {
}
int ii;
char cc;
// C Program to illustrate the use of typedef
float ff;
with
} var; // variable declaration with structure
// structures
template
#include <stdio.h>

// Driver code
// defining structure
int main()
struct str1 {
{
int a;
// variable declaration after structure
template };
// initialization with initializer list and
designated
// defining new name for str1
// initializer list
typedef struct str1 str1;
struct str1 var1 = { 1, 'A', 1.00,
"GeeksforGeeks" },
// another way of using typedef with structures
var2;
typedef struct str2 {
struct str2 var3 = { .ff = 5.00, .ii = 5, .cc = 'a'
}; int x;
} str2; int main()
{
int main() struct parent var1 = { 25, 195, 'A' };
{
// creating structure variables using new // accessing and printing nested members
names
printf("var1.a = %d\n", var1.a);
str1 var1 = { 20 };
printf("var1.b.x = %d\n", var1.b.x);
str2 var2 = { 314 };
printf("var1.b.c = %c", var1.b.c);

printf("var1.a = %d\n", var1.a);


return 0;
printf("var2.x = %d", var2.x);
}

return 0;
// C program to illustrate the structure pointer
}
#include <stdio.h>

// C Program to illustrate structure nesting along


// structure declaration
with
struct Point {
// forward declaration
int x, y;
#include <stdio.h>
};

// child structure declaration


int main()
struct child {
{
int x;
struct Point str = { 1, 2 };
char c;
};
// p2 is a pointer to structure p1
struct Point* ptr = &str;
// parent structure declaration
struct parent {
// Accessing structure members using
int a;
structure pointer
struct child b;
printf("%d %d", ptr->x, ptr->y);
};

return 0;
// driver code
}
// C program to illustrate the self referential // C program to illustrate structure padding and
structures packing
#include <stdio.h> #include <stdio.h>

// structure template // structure with padding


typedef struct str { struct str1 {
int mem1; char c;
int mem2; int i;
struct str* next; };
}str;
struct str2 {
// driver code char c;
int main() int i;
{ } __attribute((packed)) __; // using structure
packing
str var1 = { 1, 2, NULL };
str var2 = { 10, 20, NULL };
// driver code
int main()
// assigning the address of var2 to
var1.next {
var1.next = &var2;
printf("Size of str1: %d\n", sizeof(struct
str1));
// pointer to var1
printf("Size of str2: %d\n", sizeof(struct
str *ptr1 = &var1;
str2));
return 0;
// accessing var2 members using var1
}
printf("var2.mem1: %d\nvar2.mem2: %d",
// C Program to illustrate bit fields in structures
ptr1->next->mem1,
#include <stdio.h>
ptr1->next->mem2);

// declaring structure for reference


return 0;
struct str1 {
}
int a;
char c;
};
C program for array
// structure with bit fields
implementation of stack
struct str2 {
#include <limits.h>
int a : 24; // size of 'a' is 3 bytes = 24 bits
#include <stdio.h>
char c;
#include <stdlib.h>
};

// A structure to represent a stack


// driver code
struct Stack {
int main()
int top;
{
unsigned capacity;
printf("Size of Str1: %d\nSize of Str2: %d",
int* array;
sizeof(struct str1), sizeof(struct
str2)); };

return 0;
} // function to create a stack of given capacity. It
initializes size of
// stack as 0
struct Stack* createStack(unsigned capacity)
{
struct Stack* stack = (struct
Stack*)malloc(sizeof(struct Stack));
stack->capacity = capacity;
stack->top = -1;
stack->array = (int*)malloc(stack-
>capacity * sizeof(int));
return stack;
}

// Stack is full when top is equal to the last index


int isFull(struct Stack* stack)
{
return stack->top == stack->capacity - 1;
}
// Stack is empty when top is equal to -1
int isEmpty(struct Stack* stack) // Driver program to test above functions
{ int main()
return stack->top == -1; {
} struct Stack* stack = createStack(100);

// Function to add an item to stack. It increases push(stack, 10);


top by 1
push(stack, 20);
void push(struct Stack* stack, int item)
push(stack, 30);
{
if (isFull(stack))
printf("%d popped from stack\n",
return; pop(stack));
stack->array[++stack->top] = item;
printf("%d pushed to stack\n", item); return 0;
} }

// Function to remove an item from stack. It


decreases top by 1
int pop(struct Stack* stack)
{
if (isEmpty(stack))
return INT_MIN;
return stack->array[stack->top--];
}

// Function to return the top from stack without


removing it
int peek(struct Stack* stack)
{
if (isEmpty(stack))
return INT_MIN;
return stack->array[stack->top];
}

You might also like