Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kindly Approve My Requests #257

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions Sorting/Sorting using C/bubble_sorting.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include<stdio.h>
//Arastu
void bubble_sort(int, int*);
void main()
{ int n, list[20], i;
printf("input the number of elements of the list\n ");
scanf("%d",&n);
printf("\n Input the elements of the list\n");
for(i=0;i<n;i++)
{scanf("%d",&list[i]);}
printf("List Input :\n");
for(i=0;i<n;i++)
{ printf("%d\n",list[i]);
}
bubble_sort(n,list);
}
void bubble_sort(int n, int arr[])
{ int temp,i,j;
for(i=0;i<n;i++)
{ for(j=0;j<n-i-1;j++)
{
if(arr[j]>arr[j+1])
{
temp = arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
printf("\n Sorted list is as follows:\n");
for(i=0;i<n;i++)
{ printf("%d\n",arr[i]);
}
}
82 changes: 82 additions & 0 deletions Sorting/Sorting using C/heap_sorting.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#include <stdio.h>
//Arastu
void heapify(int arr[], int n, int i)
{
int temp;
int largest = i; // Initialize largest as root
int l = 2*i + 1; // left= 2*i + 1
int r = 2*i + 2; // right = 2*i + 2
// If left child is larger than root
if (l < n && arr[l] > arr[largest])
largest = l;

// If right child is larger than largest so far
if (r < n && arr[r] > arr[largest])
largest = r;

// If largest is not root
if (largest != i)
{
temp=arr[i];
arr[i]=arr[largest];
arr[largest]=temp;


// Recursively heapify the affected sub-tree
heapify(arr, n, largest);
}
}

// main function to do heap sort
void heapSort(int arr[], int n)
{
int i,temp;
// Build heap (rearrange array)
for ( i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);

//One by one extract an element from heap
for (i=n-1; i>=0; i--)
{
// Move current root to end
temp=arr[0];
arr[0]=arr[i];
arr[i]=temp;

// call max heapify on the reduced heap
heapify(arr, i, 0);
}
}

/* A utility function to print array of size n */
void printArray(int arr[], int n)
{
int i;
for ( i=0; i<n; ++i)
printf("%d\n",arr[i]);
}

// Driver program
int main()
{
int i,n;
printf("Enter the no.of elements in the array\n");
scanf("%d",&n);
int arr[n];

printf("\n Input the elements of the array\n");
for(i=0; i<n; i++)
{
scanf("%d",&arr[i]);
}
printf("Array Input :\n");
for(i=0; i<n; i++)
{
printf("%d\n",arr[i]);
}
n = sizeof(arr)/sizeof(arr[0]);
heapSort(arr, n);

printf( "Sorted array is \n");
printArray(arr, n);
}
34 changes: 34 additions & 0 deletions Sorting/Sorting using C/insertion_sorting.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include<stdio.h>
//Arastu
void insertion_sort(int, int*);
void main()
{ int n, list[20], i;
printf("input the number of elements of the list\n ");
scanf("%d",&n);
printf("\n Input the elements of the list\n");
for(i=0;i<n;i++)
{scanf("%d",&list[i]);}
printf("List Input :\n");
for(i=0;i<n;i++)
{ printf("%d\n",list[i]);
}
insertion_sort(n,list);
}

void insertion_sort(int n, int arr[])
{ int temp,i,j;
for(i=0;i<n;i++)
{ temp=arr[i];
j=i-1;
while(temp<arr[j]&&(j>=0))
{
arr[j+1]=arr[j];
j--;
}
arr[j+1]=temp;
}
printf("\n The sorted list is as follows:\n");
for(i=0;i<n;i++)
{printf("%d\n",arr[i]);
}
}
64 changes: 64 additions & 0 deletions Sorting/Sorting using C/merge _sorting.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include<stdio.h>
#include<stdlib.h>
//Arastu
void Merge(int *A,int *L,int leftCount,int *R,int rightCount) {
int i,j,k;
// i - to mark the index of left subarray (L)
// j - to mark the index of right subaraay (R)
// k - to mark the index of merged subarray (A)
i = 0; j = 0; k =0;

while(i<leftCount && j< rightCount)
{
if(L[i] < R[j]) A[k++] = L[i++];
else A[k++] = R[j++];
}

while(i < leftCount) A[k++] = L[i++];
while(j < rightCount) A[k++] = R[j++];
}

void MergeSort(int *A,int n) {
int mid,i, *L, *R;
if(n < 2) return; // base condition. If the array has less than two element, do nothing.

mid = n/2; // find the mid index.
// and (n-mid) elements (from mid to n-1) will be part of right sub-array
L = (int*)malloc(mid*sizeof(int));
R = (int*)malloc((n- mid)*sizeof(int));

for(i = 0;i<mid;i++) L[i] = A[i]; // creating left subarray
for(i = mid;i<n;i++) R[i-mid] = A[i]; // creating right subarray

MergeSort(L,mid); // sorting the left subarray
MergeSort(R,n-mid); // sorting the right subarray
Merge(A,L,mid,R,n-mid); // Merging L and R into A as sorted list.
free(L);
free(R);
}

int main() {
int i,n;
printf("Enter the no.of elements in the array\n");
scanf("%d",&n);
int A[n];

printf("\n Input the elements of the array\n");
for(i=0;i<n;i++)
{scanf("%d",&A[i]);}
printf("Array Input :\n");
for(i=0;i<n;i++)
{ printf("%d\n",A[i]);
}
/* Code to test the MergeSort function. */
n = sizeof(A)/sizeof(A[0]);

// Calling merge sort to sort the array.
MergeSort(A,n);

//printing all elements in the array once its sorted.
printf("Array Elements after sorting are:\n");
for(i = 0;i <n;i++) printf("%d element is:\t%d\n",i+1,A[i]);
return 0;

}
92 changes: 92 additions & 0 deletions Sorting/Sorting using C/mergeSort using linkedlist.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#include <stdio.h>
#include <stdlib.h>
//Arastu
typedef struct node{
int data;
struct node *next;
}Node;

void split(Node *head, Node **front, Node **back){

Node *fast = head->next;
Node *slow = head;

while(fast){
fast = fast->next;
if(fast){
fast = fast->next;
slow = slow->next;
}
}
*front = head;
*back = slow->next;
slow->next = NULL;
}

Node * merge(Node *a, Node *b){
Node *result = NULL;
if(a == NULL)
return b;
else if(b == NULL)
return a;

/* For the first node, we would set the result to either a or b */
if(a->data <= b->data){
result = a;
/* Result's next will point to smaller one in lists
starting at a->next and b */
result->next = merge(a->next,b);
}
else {
result = b;
/*Result's next will point to smaller one in lists
starting at a and b->next */
result->next = merge(a,b->next);
}
return result;
}

void mergeSort(Node **headRef){
Node * front, *back;
Node * head = *headRef;
if(head == NULL || head->next == NULL)
return;

split(head, &front, &back);
mergeSort(&front);
mergeSort(&back);

*headRef = merge(front, back);
}

void push(Node **headRef, int data){
Node * new_node = (Node *)malloc(sizeof(Node));
new_node->data = data;
new_node->next = *headRef;
*headRef = new_node;
}

void printList(Node * head){
Node * current = head;
while(current){
printf("%d -> ", current->data);
current = current->next;
}
printf("NULL");
}
int main(void) {
// your code goes here
Node *head = NULL;
Node *front, *back;
push(&head,54); push(&head,20);
push(&head,80); push(&head,157);
push(&head,239); push(&head,16);
push(&head,3); push(&head,642);
push(&head,183); push(&head,98);
push(&head,10); push(&head,74);
mergeSort(&head);
printf("\n");
printList(head);

return 0;
}
36 changes: 36 additions & 0 deletions Sorting/Sorting using C/selection_sorting.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include<stdio.h>
//Arastu
void selection_sort(int, int*);
void main()
{ int n, list[20], i;
printf("input the number of elements of the list\n ");
scanf("%d",&n);
printf("\n Input the elements of the list\n");
for(i=0;i<n;i++)
{scanf("%d",&list[i]);}
printf("List Input :\n");
for(i=0;i<n;i++)
{ printf("%d\n",list[i]);
}
selection_sort(n,list);
}

void selection_sort(int n,int arr[])
{ int min, temp, pos, i, j;
for(i=0;i<n-1;i++)
{ min =arr[i];
pos= i;
for(j=i+1;j<n;j++)
{ if(min>arr[j])
{ min= arr[j];
pos=j;
}
}
temp = arr[i];
arr[i]=arr[pos];
arr[pos]=temp;
}
printf("Entered list is as Follows:\n");
for(i=0;i<n;i++){ printf("%d\n", arr[i]);
}
}