0% found this document useful (0 votes)
39 views5 pages

Quicksort Crescator

The document describes several sorting algorithms and their implementations in C/C++ for sorting arrays in ascending or descending order. It includes implementations of quicksort, shellsort, counting sort, heap sort, bubblesort, and selection sort. Functions are provided to sort integer arrays using each of these algorithms in either ascending or descending order.

Uploaded by

Alberto Bonnuci
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views5 pages

Quicksort Crescator

The document describes several sorting algorithms and their implementations in C/C++ for sorting arrays in ascending or descending order. It includes implementations of quicksort, shellsort, counting sort, heap sort, bubblesort, and selection sort. Functions are provided to sort integer arrays using each of these algorithms in either ascending or descending order.

Uploaded by

Alberto Bonnuci
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 5

Tipuri de sortari:

Quicksort Crescator:
int quicksortCrescator(int *arr, int low, int high){
if (low<high){
int pi = partitieCrescatoare(arr, low, high);
quicksortCrescator(arr, low, pi-1);
quicksortCrescator(arr, pi+1, high);
}
return 0;
}

int partitieCrescatoare(int *arr, int low, int high){


int pivot = arr[high];
int i = (low - 1);
for(int j = low; j<= high - 1; j++){
if(arr[j] < pivot){
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i+1], &arr[high]);
return (i+1);
}

void swap(int* a, int* b)


{
int t = *a;
*a = *b;
*b = t;
}

Quicksort Descrescator:
void swap(int* a, int* b)
{
int t = *a;
*a = *b;
*b = t;
}

int quicksortDescrescator(int *arr, int low, int high){


if (low<high){
int pi = partitieDescrescatoare(arr, low, high);
quicksortDescrescator(arr, low, pi-1);
quicksortDescrescator(arr, pi+1, high);
}
return 0;
}

int partitieDescrescatoare(int *arr, int low, int high){


int pivot = arr[high];
int i = (low - 1);
for(int j = low; j<= high - 1; j++){
if(arr[j] > pivot){
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i+1], &arr[high]);
return (i+1);
}

Shell Sort Crescator


int shellSortCrescator(int *arr, int n)
{
for (int gap = n/2; gap > 0; gap /= 2)
{
for (int i = gap; i < n; i += 1)
{

int temp = arr[i];


int j;
for (j = i; j >= gap && arr[j - gap] > temp; j -= gap)
arr[j] = arr[j - gap];
arr[j] = temp;
}
}
return 0;
}

Shell Sort Descrescator.


int shellSortDescrescator(int *arr, int n)
{
for (int gap = n/2; gap > 0; gap /= 2)
{
for (int i = gap; i < n; i += 1)
{

int temp = arr[i];


int j;
for (j = i; j >= gap && arr[j - gap] < temp; j -= gap)
arr[j] = arr[j - gap];
arr[j] = temp;
}
}
return 0;
}

Counting Sort Crescator 2-D array


void countingSort(int array[m][n], int size)
{
int output[10];
int max = array[0][0];
for (int i = 1; i < size; i++)
{
if (array[i][0] > max)
max = array[i][0];
}
int count[10];
for (int i = 0; i <= max; ++i)
{
count[i] = 0;
}
for (int i = 0; i < size; i++)
{
count[array[i][0]]++;
}
for (int i = 1; i <= max; i++)
{
count[i] += count[i - 1];
}
for (int i = size - 1; i >= 0; i--)
{
output[count[array[i][0]] - 1] = array[i][0];
count[array[i][0]]--;
}
for (int i = 0; i < size; i++)
{
array[i][0] = output[i];
}
}

Heap Sort Crescator


void heapify(int arr[m][m], int n, int i)
{
int smallest = i; // Initialize smalles as root
int l = 2 * i + 1; // left = 2*i + 1
int r = 2 * i + 2; // right = 2*i + 2

// If left child is smaller than root


if (l < n && arr[l][m-1] < arr[smallest][m-1])
smallest = l;

// If right child is smaller than smallest so far


if (r < n && arr[r][m-1] < arr[smallest][m-1])
smallest = r;

// If smallest is not root


if (smallest != i) {
swap(arr[i][m-1], arr[smallest][m-1]);

// Recursively heapify the affected sub-tree


heapify(arr, n, smallest);
}
}

// main function to do heap sort


void heapSort(int arr[m][m], int n)
{
// Build heap (rearrange array)
for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);

// One by one extract an element from heap


for (int i = n - 1; i >= 0; i--) {
// Move current root to end
swap(arr[0][m-1], arr[i][m-1]);

// call max heapify on the reduced heap


heapify(arr, i, 0);
}
}

void swap(int* a, int* b)


{
int t = *a;
*a = *b;
*b = t;
}
BubbleSort Crescator
void bubbleSort(int *a){
int temp;
for(i=0; i< len-1;i++){
for(j=0; j< len- i -1;j++){
if(a[j] > a[j+1]){
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
}

SelectionSort Descrescator
void selectionSort(int *a){
int max,temp;
for(i=0;i<len;i++){
max = i;
for(j=i+1; j<len;j++){
if(a[j] > a[max])
max = j;
}
temp = a[i];
a[i] = a[max];
a[max] = temp;
}
}

You might also like