Skip to content

Heapsort/in c #319

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

Merged
merged 7 commits into from
Oct 29, 2023
Merged
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
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3029,8 +3029,8 @@ In order to achieve greater coverage and encourage more people to contribute to
<tr>
<td><a href="https://en.wikipedia.org/wiki/Heapsort">Heapsort</a></td>
<td> <!-- C -->
<a href="./CONTRIBUTING.md">
<img align="center" height="25" src="./logos/github.svg" />
<a href="./src/c/Heapsort.c">
<img align="center" height="25" src="./logos/c.svg" />
</a>
</td>
<td> <!-- C++ -->
Expand Down Expand Up @@ -3319,8 +3319,8 @@ In order to achieve greater coverage and encourage more people to contribute to
<tr>
<td><a href="https://en.wikipedia.org/wiki/Selection_sort">Selection Sort</a></td>
<td> <!-- C -->
<a href="./CONTRIBUTING.md">
<img align="center" height="25" src="./logos/github.svg" />
<a href="./src/c/SelectionSort.c">
<img align="center" height="25" src="./logos/c.svg" />
</a>
</td>
<td> <!-- C++ -->
Expand Down
66 changes: 66 additions & 0 deletions src/c/Heapsort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include <stdio.h>

void heapify(int arr[], int n, int i)
{
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;

if (left < n && arr[left] > arr[largest])
largest = left;

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

// If the largest element is not the root
if (largest != i)
{
// Swap the largest element with the root
int temp = arr[i];
arr[i] = arr[largest];
arr[largest] = temp;

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

void heapsort(int arr[], int n)
{
// Build a max heap
int i;
for (i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);

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

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

int main()
{
int arr[] = {12, 21, 13, 5, 1, 7};
int i;
int n = sizeof(arr) / sizeof(arr[0]);

printf("Original array: ");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);

heapsort(arr, n);

printf("\nSorted array: ");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);

return 0;
}
41 changes: 41 additions & 0 deletions src/c/SelectionSort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <stdio.h>

void swap(int array[], int i, int j)
{
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}

void selection_sort(int array[], int n)
{
int min, i, j;
for (i = 0; i < n; i++)
{
min = i;
for (j = i + 1; j < n; j++)
{
if (array[min] > array[j])
{
min = j;
}
}
if (min != i)
swap(array, min, i);
}
}

int main()
{
int array_size = 10;
int array[10] = {45, 7, 125, 18, 3, 5, 11, 107, 60, 4};

selection_sort(array, array_size);

printf("Sorted Array:\n");
int i;
for (i = 0; i < array_size; i++)
printf("%d ", array[i]);

return 0;
}