C Programming
C Programming
SORTING }
}
} return loc;
} }
int par(int a[],int beg,int end)
int temp=a[loc];
Quick sort with last element as pivot
a[loc]=a[right]; #include <stdio.h>
a[right]=temp;
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;
}
}
while (1) { }
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];
}
return 0; }
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
a[middle] = a[end];
while (left <= right && a[right] > pivot) { qs(a, loc + 1, end);
right--; }
} }
}
// 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};
return 0; }
} }
return loc;
left = loc = beg; void qs(int a[], int beg, int end) {
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 main()
if (flag != 1) { {
while (a[loc] >= a[left] && (loc != left)) { int a[]= {15,2,7,54,84,88,53,9};
flag = 1; printf("\n");
} else { is(a,n);
} }
insertionSort(arr, n);
{ }
key=a[i];
j=i-1; return 0;
} key = arr[i];
a[j+1]=key; j = i - 1;
arr[j + 1] = arr[j];
outputs }
}
Merge sort with intermediate }
outputs }
int mid;
void merge(int a[], int beg, int mid, int end) { if (beg < end) {
} else { }
j++; }
} }
index++;
} int main() {
j++; }
index++;
a[k] = temp[k];
if (left < N && arr[left] > arr[largest])
// so far
// Function to swap the position of two elements if (right < N && arr[right] > arr[largest])
swap(&arr[i], &arr[largest]);
// To heapify a subtree rooted with node i
// Heap sort
// If left child is larger than root for (int i = N - 1; i >= 0; i--) {
swap(&arr[0], &arr[i]);
// root again
heapify(arr, i, 0);
printf("\n");
// Driver's code
int main()
// Function call
heapSort(arr, N);
printArray(arr, N);
SEARCHING }
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;
}
}
}
if (arr[mid] < x)
}
// Stack is empty when top is equal to -1
#include <stdlib.h>
// Function to add an item to stack. It increases top by
1
struct 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;
}
}
push(stack, 10);
push(stack, 20);
push(stack, 30);
return 0;
}
C program to illustrate
// copying structure using assignment
the use of structures operator
var2 = var1;
#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);
return 0;
// C program to illustrate the structure pointer
}
#include <stdio.h>
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>
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;
}