mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
use malloc and free for dynamic variables
This commit is contained in:
parent
dee56f7781
commit
4b07f0f6fc
@ -24,6 +24,7 @@ set(CMAKE_C_STANDARD_REQUIRED ON)
|
|||||||
|
|
||||||
if(MSVC)
|
if(MSVC)
|
||||||
add_compile_definitions(_CRT_SECURE_NO_WARNINGS)
|
add_compile_definitions(_CRT_SECURE_NO_WARNINGS)
|
||||||
|
add_compile_options(/Za)
|
||||||
endif(MSVC)
|
endif(MSVC)
|
||||||
|
|
||||||
add_subdirectory(conversions)
|
add_subdirectory(conversions)
|
||||||
|
@ -3,13 +3,17 @@
|
|||||||
//generate notes for an entered amount N.
|
//generate notes for an entered amount N.
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
int ways(int n, int a[], int k)
|
int ways(int n, int *a, int k)
|
||||||
{
|
{
|
||||||
if(n<0 || k<0) return 0;
|
if (n < 0 || k < 0)
|
||||||
if(n == 0) return 1;
|
return 0;
|
||||||
if(k == 0) return 0;
|
if (n == 0)
|
||||||
return ways(n, a, k-1) + ways(n-a[k-1], a, k);
|
return 1;
|
||||||
|
if (k == 0)
|
||||||
|
return 0;
|
||||||
|
return ways(n, a, k - 1) + ways(n - a[k - 1], a, k);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
@ -20,15 +24,15 @@ int main()
|
|||||||
|
|
||||||
printf("Number of coins? ");
|
printf("Number of coins? ");
|
||||||
scanf("%d", &m);
|
scanf("%d", &m);
|
||||||
int coin[m], i;
|
int *coin = (int *)malloc(m * sizeof(int)), i;
|
||||||
for(i=0; i<m; i++)
|
for (i = 0; i < m; i++)
|
||||||
{
|
{
|
||||||
printf("coin? ");
|
printf("coin? ");
|
||||||
scanf("%d", &coin[i]);
|
scanf("%d", &coin[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("---- your requests --- \n");
|
printf("---- your requests --- \n");
|
||||||
while(1)
|
while (1)
|
||||||
{
|
{
|
||||||
printf("amount? exit(0) ");
|
printf("amount? exit(0) ");
|
||||||
scanf("%d", &n);
|
scanf("%d", &n);
|
||||||
@ -38,5 +42,7 @@ int main()
|
|||||||
}
|
}
|
||||||
printf("%d\n", ways(n, coin, m));
|
printf("%d\n", ways(n, coin, m));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
free(coin);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -2,52 +2,61 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
void swap(char* left, char* right){
|
void swap(char *left, char *right)
|
||||||
|
{
|
||||||
char temp = *left;
|
char temp = *left;
|
||||||
*left = *right;
|
*left = *right;
|
||||||
*right = temp;
|
*right = temp;
|
||||||
}
|
}
|
||||||
|
|
||||||
int compare (const void * a, const void * b){
|
int compare(const void *a, const void *b)
|
||||||
return ( *(char*)a - *(char*)b );
|
{
|
||||||
|
return (*(char *)a - *(char *)b);
|
||||||
}
|
}
|
||||||
|
|
||||||
void PrintSortedPermutations(char str[])
|
void PrintSortedPermutations(char *str)
|
||||||
{
|
{
|
||||||
int strSize = strlen(str);
|
int strSize = strlen(str);
|
||||||
qsort(str, strSize, sizeof(char), compare);
|
qsort(str, strSize, sizeof(char), compare);
|
||||||
|
|
||||||
int largerPermFound = 1;
|
int largerPermFound = 1;
|
||||||
do{
|
do
|
||||||
|
{
|
||||||
// 1. Print permutation
|
// 1. Print permutation
|
||||||
printf("%s\n", str);
|
printf("%s\n", str);
|
||||||
// 2. Find rightmost char that is smaller than char to its right
|
// 2. Find rightmost char that is smaller than char to its right
|
||||||
int i;
|
int i;
|
||||||
for (i = strSize - 2; i >= 0 && str[i] >= str[i+1]; --i){}
|
for (i = strSize - 2; i >= 0 && str[i] >= str[i + 1]; --i)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
// if we couldn't find one, we're finished, else we can swap
|
// if we couldn't find one, we're finished, else we can swap
|
||||||
if (i >= 0){
|
if (i >= 0)
|
||||||
|
{
|
||||||
// 3. find character at index j such that str[j] = min(str[k]) && str[k] > str[i] for all k > i
|
// 3. find character at index j such that str[j] = min(str[k]) && str[k] > str[i] for all k > i
|
||||||
int j = i+1, k;
|
int j = i + 1, k;
|
||||||
for(k=j; k<strSize && str[k]; k++){
|
for (k = j; k < strSize && str[k]; k++)
|
||||||
|
{
|
||||||
if (str[k] > str[i] && str[k] < str[j])
|
if (str[k] > str[i] && str[k] < str[j])
|
||||||
j = k;
|
j = k;
|
||||||
}
|
}
|
||||||
// 3. Swap chars at i and j
|
// 3. Swap chars at i and j
|
||||||
swap(&str[i], &str[j]);
|
swap(&str[i], &str[j]);
|
||||||
// 4. Sort string to the right of i
|
// 4. Sort string to the right of i
|
||||||
qsort(str+i+1, strSize-i-1, sizeof(char), compare);
|
qsort(str + i + 1, strSize - i - 1, sizeof(char), compare);
|
||||||
}
|
}
|
||||||
else largerPermFound = 0;
|
else
|
||||||
}
|
largerPermFound = 0;
|
||||||
while(largerPermFound);
|
} while (largerPermFound);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main()
|
||||||
|
{
|
||||||
int n; //size of string
|
int n; //size of string
|
||||||
scanf("%d\n",&n);
|
scanf("%d\n", &n);
|
||||||
char str[n];
|
char *str = (char *)malloc(n * sizeof(char));
|
||||||
scanf("%s",str);
|
scanf("%s", str);
|
||||||
PrintSortedPermutations(str);
|
PrintSortedPermutations(str);
|
||||||
|
free(str);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -8,70 +8,86 @@
|
|||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
const int M=144;
|
#define M 144
|
||||||
int N, R, C;
|
int N, R, C;
|
||||||
|
|
||||||
int OKrow(int a[M], int x, int y, int v) {
|
int OKrow(int a[M], int x, int y, int v)
|
||||||
|
{
|
||||||
int j;
|
int j;
|
||||||
for(j=0; j<N; j++)
|
for (j = 0; j < N; j++)
|
||||||
if(a[x*N+j] == v)
|
if (a[x * N + j] == v)
|
||||||
return 0;
|
return 0;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
int OKcol(int a[M], int x, int y, int v) {
|
int OKcol(int a[M], int x, int y, int v)
|
||||||
|
{
|
||||||
int i;
|
int i;
|
||||||
for(i=0; i<N; i++)
|
for (i = 0; i < N; i++)
|
||||||
if(a[i*N+y] == v)
|
if (a[i * N + y] == v)
|
||||||
return 0;
|
return 0;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
int OKbox(int a[M], int x, int y, int v) {
|
int OKbox(int a[M], int x, int y, int v)
|
||||||
int bi=x/R, bj=y/C, i, j;
|
{
|
||||||
for(i=0; i<R; i++)
|
int bi = x / R, bj = y / C, i, j;
|
||||||
for(j=0; j<C; j++)
|
for (i = 0; i < R; i++)
|
||||||
if(a[(i + bi*R)*N + (j + bj*C)] == v)
|
for (j = 0; j < C; j++)
|
||||||
|
if (a[(i + bi * R) * N + (j + bj * C)] == v)
|
||||||
return 0;
|
return 0;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
int OK(int a[M], int x, int y, int v) {
|
int OK(int a[M], int x, int y, int v)
|
||||||
return OKrow(a,x,y,v) && OKcol(a,x,y,v) && OKbox(a,x,y,v);
|
{
|
||||||
|
return OKrow(a, x, y, v) && OKcol(a, x, y, v) && OKbox(a, x, y, v);
|
||||||
}
|
}
|
||||||
|
|
||||||
void print(int a[M]) {
|
void print(int a[M])
|
||||||
|
{
|
||||||
int i, j;
|
int i, j;
|
||||||
for(i=0; i<N; i++)
|
for (i = 0; i < N; i++)
|
||||||
for(j=0; j<N; j++)
|
for (j = 0; j < N; j++)
|
||||||
printf("%d%c", a[i*N+j], (j==N-1 ? '\n':' '));
|
printf("%d%c", a[i * N + j], (j == N - 1 ? '\n' : ' '));
|
||||||
}
|
}
|
||||||
|
|
||||||
int solve(int a[M]) {
|
int solve(int a[M])
|
||||||
int i, j, v, rem=0;
|
{
|
||||||
for(i=0; i<N; i++) {
|
int i, j, v, rem = 0;
|
||||||
for(j=0; j<N; j++) {
|
for (i = 0; i < N; i++)
|
||||||
if(a[i*N+j] == 0) {
|
{
|
||||||
|
for (j = 0; j < N; j++)
|
||||||
|
{
|
||||||
|
if (a[i * N + j] == 0)
|
||||||
|
{
|
||||||
rem = 1;
|
rem = 1;
|
||||||
for(v=1; v<=N; v++) {
|
for (v = 1; v <= N; v++)
|
||||||
if(OK(a,i,j,v)) {
|
{
|
||||||
a[i*N+j] = v;
|
if (OK(a, i, j, v))
|
||||||
if(solve(a)) return 1;
|
{
|
||||||
a[i*N+j] = 0;
|
a[i * N + j] = v;
|
||||||
|
if (solve(a))
|
||||||
|
return 1;
|
||||||
|
a[i * N + j] = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(rem == 0) return 1;
|
if (rem == 0)
|
||||||
|
return 1;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main()
|
||||||
|
{
|
||||||
scanf("%d%d%d", &N, &R, &C);
|
scanf("%d%d%d", &N, &R, &C);
|
||||||
int a[M], i, j;
|
int a[M], i, j;
|
||||||
for(i=0; i<N; i++)
|
for (i = 0; i < N; i++)
|
||||||
for(j=0; j<N; j++)
|
for (j = 0; j < N; j++)
|
||||||
scanf("%d", &a[i*N+j]);
|
scanf("%d", &a[i * N + j]);
|
||||||
|
|
||||||
if(solve(a)) print(a);
|
if (solve(a))
|
||||||
else printf("Invalid\n");
|
print(a);
|
||||||
|
else
|
||||||
|
printf("Invalid\n");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -22,7 +22,7 @@
|
|||||||
/**
|
/**
|
||||||
* define polynomial function
|
* define polynomial function
|
||||||
**/
|
**/
|
||||||
long double complex function(double *coeffs, unsigned int degree, long double complex x)
|
long double complex poly_function(double *coeffs, unsigned int degree, long double complex x)
|
||||||
{
|
{
|
||||||
long double complex out = 0.;
|
long double complex out = 0.;
|
||||||
unsigned int n;
|
unsigned int n;
|
||||||
@ -154,7 +154,7 @@ int main(int argc, char **argv)
|
|||||||
|
|
||||||
for (n = 0; n < degree - 1; n++)
|
for (n = 0; n < degree - 1; n++)
|
||||||
{
|
{
|
||||||
long double complex numerator = function(coeffs, degree, s0[n]);
|
long double complex numerator = poly_function(coeffs, degree, s0[n]);
|
||||||
long double complex denominator = 1.0;
|
long double complex denominator = 1.0;
|
||||||
for (i = 0; i < degree - 1; i++)
|
for (i = 0; i < degree - 1; i++)
|
||||||
if (i != n)
|
if (i != n)
|
||||||
|
@ -1,18 +1,20 @@
|
|||||||
#include<stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
int n,m; //size of the matrix
|
int n, m; //size of the matrix
|
||||||
|
|
||||||
// This function does Binary search for x in i-th row from j_low to j_high.
|
// This function does Binary search for x in i-th row from j_low to j_high.
|
||||||
void binarySearch(int mat[n][m], int i, int j_low,int j_high, int x)
|
void binarySearch(int **mat, int i, int j_low, int j_high, int x)
|
||||||
{
|
{
|
||||||
while (j_low <= j_high)
|
while (j_low <= j_high)
|
||||||
{
|
{
|
||||||
int j_mid = (j_low + j_high) / 2;
|
int j_mid = (j_low + j_high) / 2;
|
||||||
|
|
||||||
// Element found
|
// Element found
|
||||||
if (mat[i][j_mid] == x){
|
if (mat[i][j_mid] == x)
|
||||||
printf("Found at (%d,%d)\n",i,j_mid);
|
{
|
||||||
return ;
|
printf("Found at (%d,%d)\n", i, j_mid);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
else if (mat[i][j_mid] > x)
|
else if (mat[i][j_mid] > x)
|
||||||
j_high = j_mid - 1;
|
j_high = j_mid - 1;
|
||||||
@ -22,25 +24,27 @@ void binarySearch(int mat[n][m], int i, int j_low,int j_high, int x)
|
|||||||
// element not found
|
// element not found
|
||||||
printf("element not found\n");
|
printf("element not found\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Function to perform binary search on the mid values of row to get the desired pair of rows
|
// Function to perform binary search on the mid values of row to get the desired pair of rows
|
||||||
// where the element can be found
|
// where the element can be found
|
||||||
void modifiedBinarySearch(int mat[n][m], int n, int m, int x)
|
void modifiedBinarySearch(int **mat, int n, int m, int x)
|
||||||
{ // If Single row matrix
|
{ // If Single row matrix
|
||||||
if (n == 1){
|
if (n == 1)
|
||||||
binarySearch(mat, 0, 0, m-1, x);
|
{
|
||||||
|
binarySearch(mat, 0, 0, m - 1, x);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Do binary search in middle column.
|
// Do binary search in middle column.
|
||||||
// Condition to terminate the loop when the 2 desired rows are found.
|
// Condition to terminate the loop when the 2 desired rows are found.
|
||||||
int i_low = 0, i_high = n-1, j_mid = m/2;
|
int i_low = 0, i_high = n - 1, j_mid = m / 2;
|
||||||
while ((i_low+1) < i_high)
|
while ((i_low + 1) < i_high)
|
||||||
{
|
{
|
||||||
int i_mid = (i_low + i_high) / 2;
|
int i_mid = (i_low + i_high) / 2;
|
||||||
// element found
|
// element found
|
||||||
if (mat[i_mid][j_mid] == x){
|
if (mat[i_mid][j_mid] == x)
|
||||||
printf("Found at (%d,%d)\n",i_mid,j_mid);
|
{
|
||||||
|
printf("Found at (%d,%d)\n", i_mid, j_mid);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
else if (mat[i_mid][j_mid] > x)
|
else if (mat[i_mid][j_mid] > x)
|
||||||
@ -50,37 +54,46 @@ void modifiedBinarySearch(int mat[n][m], int n, int m, int x)
|
|||||||
}
|
}
|
||||||
// If element is present on the mid of the two rows
|
// If element is present on the mid of the two rows
|
||||||
if (mat[i_low][j_mid] == x)
|
if (mat[i_low][j_mid] == x)
|
||||||
printf("Found at (%d,%d)\n",i_low,j_mid);
|
printf("Found at (%d,%d)\n", i_low, j_mid);
|
||||||
else if (mat[i_low+1][j_mid] == x)
|
else if (mat[i_low + 1][j_mid] == x)
|
||||||
printf("Found at (%d,%d)\n",i_low+1,j_mid);
|
printf("Found at (%d,%d)\n", i_low + 1, j_mid);
|
||||||
|
|
||||||
// Search element on 1st half of 1st row
|
// Search element on 1st half of 1st row
|
||||||
else if (x <= mat[i_low][j_mid-1])
|
else if (x <= mat[i_low][j_mid - 1])
|
||||||
binarySearch(mat, i_low, 0, j_mid-1, x);
|
binarySearch(mat, i_low, 0, j_mid - 1, x);
|
||||||
|
|
||||||
// Search element on 2nd half of 1st row
|
// Search element on 2nd half of 1st row
|
||||||
else if (x >= mat[i_low][j_mid+1] && x <= mat[i_low][m-1])
|
else if (x >= mat[i_low][j_mid + 1] && x <= mat[i_low][m - 1])
|
||||||
binarySearch(mat, i_low, j_mid+1, m-1, x);
|
binarySearch(mat, i_low, j_mid + 1, m - 1, x);
|
||||||
|
|
||||||
// Search element on 1st half of 2nd row
|
// Search element on 1st half of 2nd row
|
||||||
else if (x <= mat[i_low+1][j_mid-1])
|
else if (x <= mat[i_low + 1][j_mid - 1])
|
||||||
binarySearch(mat, i_low+1, 0, j_mid-1, x);
|
binarySearch(mat, i_low + 1, 0, j_mid - 1, x);
|
||||||
|
|
||||||
// search element on 2nd half of 2nd row
|
// search element on 2nd half of 2nd row
|
||||||
else
|
else
|
||||||
binarySearch(mat, i_low+1, j_mid+1, m-1, x);
|
binarySearch(mat, i_low + 1, j_mid + 1, m - 1, x);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
int x; //element to be searched
|
int x; //element to be searched
|
||||||
scanf("%d %d %d\n",&n,&m,&x);
|
scanf("%d %d %d\n", &n, &m, &x);
|
||||||
int mat[n][m];
|
int **mat = (int **)malloc(n * sizeof(int *));
|
||||||
for(int i=0; i<n; i++){
|
for (x = 0; x < n; x++)
|
||||||
for(int j=0; j<m; j++){
|
mat[x] = (int *)malloc(m * sizeof(int));
|
||||||
scanf("%d",&mat[i][j]);
|
|
||||||
|
for (int i = 0; i < n; i++)
|
||||||
|
{
|
||||||
|
for (int j = 0; j < m; j++)
|
||||||
|
{
|
||||||
|
scanf("%d", &mat[i][j]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
modifiedBinarySearch(mat, n, m, x);
|
modifiedBinarySearch(mat, n, m, x);
|
||||||
|
|
||||||
|
for (x = 0; x < n; x++)
|
||||||
|
free(mat[x]);
|
||||||
|
free(mat);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -1,37 +1,43 @@
|
|||||||
//sorting of array list using selection sort
|
//sorting of array list using selection sort
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
/*Displays the array, passed to this method*/
|
/*Displays the array, passed to this method*/
|
||||||
void display(int arr[], int n){
|
void display(int *arr, int n)
|
||||||
|
{
|
||||||
|
|
||||||
int i;
|
int i;
|
||||||
for(i = 0; i < n; i++){
|
for (i = 0; i < n; i++)
|
||||||
|
{
|
||||||
printf("%d ", arr[i]);
|
printf("%d ", arr[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*Swap function to swap two values*/
|
/*Swap function to swap two values*/
|
||||||
void swap(int *first, int *second){
|
void swap(int *first, int *second)
|
||||||
|
{
|
||||||
|
|
||||||
int temp = *first;
|
int temp = *first;
|
||||||
*first = *second;
|
*first = *second;
|
||||||
*second = temp;
|
*second = temp;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*This is where the sorting of the array takes place
|
/*This is where the sorting of the array takes place
|
||||||
arr[] --- Array to be sorted
|
arr[] --- Array to be sorted
|
||||||
size --- Array Size
|
size --- Array Size
|
||||||
*/
|
*/
|
||||||
void selectionSort(int arr[], int size){
|
void selectionSort(int *arr, int size)
|
||||||
|
{
|
||||||
for(int i=0; i<size; i++) {
|
|
||||||
|
for (int i = 0; i < size; i++)
|
||||||
|
{
|
||||||
int min_index = i;
|
int min_index = i;
|
||||||
for(int j=i+1; j<size; j++) {
|
for (int j = i + 1; j < size; j++)
|
||||||
if(arr[min_index] > arr[j]) {
|
{
|
||||||
|
if (arr[min_index] > arr[j])
|
||||||
|
{
|
||||||
min_index = j;
|
min_index = j;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -39,26 +45,28 @@ void selectionSort(int arr[], int size){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, const char * argv[]) {
|
int main(int argc, const char *argv[])
|
||||||
|
{
|
||||||
int n;
|
int n;
|
||||||
printf("Enter size of array:\n");
|
printf("Enter size of array:\n");
|
||||||
scanf("%d", &n); // E.g. 8
|
scanf("%d", &n); // E.g. 8
|
||||||
|
|
||||||
printf("Enter the elements of the array\n");
|
printf("Enter the elements of the array\n");
|
||||||
int i;
|
int i;
|
||||||
int arr[n];
|
int *arr = (int *)malloc(n * sizeof(int));
|
||||||
for(i = 0; i < n; i++){
|
for (i = 0; i < n; i++)
|
||||||
scanf("%d", &arr[i] );
|
{
|
||||||
|
scanf("%d", &arr[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("Original array: ");
|
printf("Original array: ");
|
||||||
display(arr, n); // Original array : 10 11 9 8 4 7 3 8
|
display(arr, n); // Original array : 10 11 9 8 4 7 3 8
|
||||||
|
|
||||||
selectionSort(arr, n);
|
selectionSort(arr, n);
|
||||||
|
|
||||||
printf("Sorted array: ");
|
printf("Sorted array: ");
|
||||||
display(arr, n); // Sorted array : 3 4 7 8 8 9 10 11
|
display(arr, n); // Sorted array : 3 4 7 8 8 9 10 11
|
||||||
|
|
||||||
|
free(arr);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,42 +2,48 @@
|
|||||||
* Using binary search to find the proper location for
|
* Using binary search to find the proper location for
|
||||||
* inserting the selected item at each iteration. */
|
* inserting the selected item at each iteration. */
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
/*Displays the array, passed to this method*/
|
/*Displays the array, passed to this method*/
|
||||||
void display(int arr[], int n) {
|
void display(int *arr, int n)
|
||||||
|
{
|
||||||
int i;
|
int i;
|
||||||
for(i = 0; i < n; i++){
|
for (i = 0; i < n; i++)
|
||||||
|
{
|
||||||
printf("%d ", arr[i]);
|
printf("%d ", arr[i]);
|
||||||
}
|
}
|
||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
int binarySearch(int arr[], int key, int low, int high) {
|
int binarySearch(int *arr, int key, int low, int high)
|
||||||
|
{
|
||||||
if (low >= high)
|
if (low >= high)
|
||||||
return (key > arr[low]) ? (low + 1): low;
|
return (key > arr[low]) ? (low + 1) : low;
|
||||||
int mid = low + (high - 1) / 2;
|
int mid = low + (high - 1) / 2;
|
||||||
if(arr[mid] == key)
|
if (arr[mid] == key)
|
||||||
return mid + 1;
|
return mid + 1;
|
||||||
else if (arr[mid] > key)
|
else if (arr[mid] > key)
|
||||||
return binarySearch(arr, key, low, mid - 1);
|
return binarySearch(arr, key, low, mid - 1);
|
||||||
else
|
else
|
||||||
return binarySearch(arr, key, mid + 1, high);
|
return binarySearch(arr, key, mid + 1, high);
|
||||||
|
|
||||||
}
|
}
|
||||||
/*This is where the sorting of the array takes place
|
/*This is where the sorting of the array takes place
|
||||||
arr[] --- Array to be sorted
|
arr[] --- Array to be sorted
|
||||||
size --- Array Size
|
size --- Array Size
|
||||||
*/
|
*/
|
||||||
void insertionSort(int arr[], int size) {
|
void insertionSort(int *arr, int size)
|
||||||
|
{
|
||||||
int i, j, key, index;
|
int i, j, key, index;
|
||||||
for(i = 0; i < size; i++) {
|
for (i = 0; i < size; i++)
|
||||||
|
{
|
||||||
j = i - 1;
|
j = i - 1;
|
||||||
key = arr[i];
|
key = arr[i];
|
||||||
/* Use binrary search to find exact key's index */
|
/* Use binrary search to find exact key's index */
|
||||||
index = binarySearch(arr, key, 0, j);
|
index = binarySearch(arr, key, 0, j);
|
||||||
/* Move all elements greater than key from [index...j]
|
/* Move all elements greater than key from [index...j]
|
||||||
* to one position */
|
* to one position */
|
||||||
while(j >= index) {
|
while (j >= index)
|
||||||
|
{
|
||||||
arr[j + 1] = arr[j];
|
arr[j + 1] = arr[j];
|
||||||
j = j - 1;
|
j = j - 1;
|
||||||
}
|
}
|
||||||
@ -46,16 +52,18 @@ void insertionSort(int arr[], int size) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, const char * argv[]) {
|
int main(int argc, const char *argv[])
|
||||||
|
{
|
||||||
int n;
|
int n;
|
||||||
printf("Enter size of array:\n");
|
printf("Enter size of array:\n");
|
||||||
scanf("%d", &n); // E.g. 8
|
scanf("%d", &n); // E.g. 8
|
||||||
|
|
||||||
printf("Enter the elements of the array\n");
|
printf("Enter the elements of the array\n");
|
||||||
int i;
|
int i;
|
||||||
int arr[n];
|
int *arr = (int *)malloc(n * sizeof(int));
|
||||||
for(i = 0; i < n; i++) {
|
for (i = 0; i < n; i++)
|
||||||
scanf("%d", &arr[i] );
|
{
|
||||||
|
scanf("%d", &arr[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("Original array: ");
|
printf("Original array: ");
|
||||||
@ -66,6 +74,6 @@ int main(int argc, const char * argv[]) {
|
|||||||
printf("Sorted array: ");
|
printf("Sorted array: ");
|
||||||
display(arr, n);
|
display(arr, n);
|
||||||
|
|
||||||
|
free(arr);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,6 +7,7 @@
|
|||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
@ -15,26 +16,26 @@ int main()
|
|||||||
printf("Enter size of array = ");
|
printf("Enter size of array = ");
|
||||||
scanf("%d", &n);
|
scanf("%d", &n);
|
||||||
|
|
||||||
int a[n];
|
int *a = (int *)malloc(n * sizeof(int));
|
||||||
printf("Enter %d elements in array :\n", n);
|
printf("Enter %d elements in array :\n", n);
|
||||||
for(i = 0; i < n; i++)
|
for (i = 0; i < n; i++)
|
||||||
{
|
{
|
||||||
scanf("%d", &a[i]);
|
scanf("%d", &a[i]);
|
||||||
if(a[i] > l)
|
if (a[i] > l)
|
||||||
l = a[i];
|
l = a[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
int b[l + 1];
|
int b[l + 1];
|
||||||
memset(b, 0, (l + 1) * sizeof(b[0]));
|
memset(b, 0, (l + 1) * sizeof(b[0]));
|
||||||
|
|
||||||
for(i = 0; i < n; i++)
|
for (i = 0; i < n; i++)
|
||||||
b[a[i]]++; //hashing number to array index
|
b[a[i]]++; //hashing number to array index
|
||||||
|
|
||||||
for(i = 0; i < (l + 1); i++) //unstable , stabilized by prefix sum array
|
for (i = 0; i < (l + 1); i++) //unstable , stabilized by prefix sum array
|
||||||
{
|
{
|
||||||
if(b[i] > 0)
|
if (b[i] > 0)
|
||||||
{
|
{
|
||||||
while(b[i] != 0) //for case when number exists more than once
|
while (b[i] != 0) //for case when number exists more than once
|
||||||
{
|
{
|
||||||
printf("%d ", i);
|
printf("%d ", i);
|
||||||
b[i]--;
|
b[i]--;
|
||||||
@ -42,5 +43,6 @@ int main()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
free(a);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,13 @@
|
|||||||
//sorting of array list using insertion sort
|
//sorting of array list using insertion sort
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
/*Displays the array, passed to this method*/
|
/*Displays the array, passed to this method*/
|
||||||
void display(int arr[], int n) {
|
void display(int *arr, int n)
|
||||||
|
{
|
||||||
int i;
|
int i;
|
||||||
for(i = 0; i < n; i++){
|
for (i = 0; i < n; i++)
|
||||||
|
{
|
||||||
printf("%d ", arr[i]);
|
printf("%d ", arr[i]);
|
||||||
}
|
}
|
||||||
printf("\n");
|
printf("\n");
|
||||||
@ -14,13 +17,16 @@ void display(int arr[], int n) {
|
|||||||
arr[] --- Array to be sorted
|
arr[] --- Array to be sorted
|
||||||
size --- Array Size
|
size --- Array Size
|
||||||
*/
|
*/
|
||||||
void insertionSort(int arr[], int size) {
|
void insertionSort(int *arr, int size)
|
||||||
|
{
|
||||||
int i, j, key;
|
int i, j, key;
|
||||||
for(i = 0; i < size; i++) {
|
for (i = 0; i < size; i++)
|
||||||
|
{
|
||||||
j = i - 1;
|
j = i - 1;
|
||||||
key = arr[i];
|
key = arr[i];
|
||||||
/* Move all elements greater than key to one position */
|
/* Move all elements greater than key to one position */
|
||||||
while(j >= 0 && key < arr[j]) {
|
while (j >= 0 && key < arr[j])
|
||||||
|
{
|
||||||
arr[j + 1] = arr[j];
|
arr[j + 1] = arr[j];
|
||||||
j = j - 1;
|
j = j - 1;
|
||||||
}
|
}
|
||||||
@ -29,16 +35,18 @@ void insertionSort(int arr[], int size) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, const char * argv[]) {
|
int main(int argc, const char *argv[])
|
||||||
|
{
|
||||||
int n;
|
int n;
|
||||||
printf("Enter size of array:\n");
|
printf("Enter size of array:\n");
|
||||||
scanf("%d", &n); // E.g. 8
|
scanf("%d", &n); // E.g. 8
|
||||||
|
|
||||||
printf("Enter the elements of the array\n");
|
printf("Enter the elements of the array\n");
|
||||||
int i;
|
int i;
|
||||||
int arr[n];
|
int *arr = (int *)malloc(n * sizeof(int));
|
||||||
for(i = 0; i < n; i++) {
|
for (i = 0; i < n; i++)
|
||||||
scanf("%d", &arr[i] );
|
{
|
||||||
|
scanf("%d", &arr[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("Original array: ");
|
printf("Original array: ");
|
||||||
@ -49,6 +57,6 @@ int main(int argc, const char * argv[]) {
|
|||||||
printf("Sorted array: ");
|
printf("Sorted array: ");
|
||||||
display(arr, n);
|
display(arr, n);
|
||||||
|
|
||||||
|
free(arr);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,10 +1,11 @@
|
|||||||
//sorting of array list using Radix sort
|
//sorting of array list using Radix sort
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
#define range 10 // Range for integers is 10 as digits range from 0-9
|
#define range 10 // Range for integers is 10 as digits range from 0-9
|
||||||
|
|
||||||
// Utility function to get the maximum value in ar[]
|
// Utility function to get the maximum value in ar[]
|
||||||
int MAX(int ar[], int size)
|
int MAX(int *ar, int size)
|
||||||
{
|
{
|
||||||
int i, max = ar[0];
|
int i, max = ar[0];
|
||||||
for (i = 0; i < size; i++)
|
for (i = 0; i < size; i++)
|
||||||
@ -16,7 +17,7 @@ int MAX(int ar[], int size)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Counting sort according to the digit represented by place
|
// Counting sort according to the digit represented by place
|
||||||
void countSort(int arr[], int n, int place)
|
void countSort(int *arr, int n, int place)
|
||||||
{
|
{
|
||||||
int i, freq[range] = {0};
|
int i, freq[range] = {0};
|
||||||
int output[n];
|
int output[n];
|
||||||
@ -46,7 +47,7 @@ void countSort(int arr[], int n, int place)
|
|||||||
n --- Array Size
|
n --- Array Size
|
||||||
max --- Maximum element in Array
|
max --- Maximum element in Array
|
||||||
*/
|
*/
|
||||||
void radixsort(int arr[], int n, int max) //max is the maximum element in the array
|
void radixsort2(int *arr, int n, int max) //max is the maximum element in the array
|
||||||
{
|
{
|
||||||
int mul = 1;
|
int mul = 1;
|
||||||
while (max)
|
while (max)
|
||||||
@ -72,7 +73,7 @@ int main(int argc, const char *argv[])
|
|||||||
|
|
||||||
printf("Enter the elements of the array\n");
|
printf("Enter the elements of the array\n");
|
||||||
int i;
|
int i;
|
||||||
int arr[n];
|
int *arr = (int *)malloc(n * sizeof(int));
|
||||||
for (i = 0; i < n; i++)
|
for (i = 0; i < n; i++)
|
||||||
{
|
{
|
||||||
scanf("%d", &arr[i]);
|
scanf("%d", &arr[i]);
|
||||||
@ -84,10 +85,11 @@ int main(int argc, const char *argv[])
|
|||||||
int max;
|
int max;
|
||||||
max = MAX(arr, n);
|
max = MAX(arr, n);
|
||||||
|
|
||||||
radixsort(arr, n, max);
|
radixsort2(arr, n, max);
|
||||||
|
|
||||||
printf("Sorted array: ");
|
printf("Sorted array: ");
|
||||||
display(arr, n); // Sorted array : 3 4 7 8 8 9 10 11
|
display(arr, n); // Sorted array : 3 4 7 8 8 9 10 11
|
||||||
|
|
||||||
|
free(arr);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -5,23 +5,23 @@ This can take time O(n*n) to sort in the worst case.
|
|||||||
Now in randomised quick sort, pivot is randomly chosen and then recursively sort the left and right sub-arrays.
|
Now in randomised quick sort, pivot is randomly chosen and then recursively sort the left and right sub-arrays.
|
||||||
The expected running time of the algorithm is O(nlog(n)).
|
The expected running time of the algorithm is O(nlog(n)).
|
||||||
*/
|
*/
|
||||||
#include<stdio.h>
|
#include <stdio.h>
|
||||||
#include<stdlib.h>
|
#include <stdlib.h>
|
||||||
#include<time.h>
|
#include <time.h>
|
||||||
|
|
||||||
int getBig(int a[], int i, int right, int pivot)
|
int getBig(int *a, int i, int right, int pivot)
|
||||||
{
|
{
|
||||||
for(int k = i; k <= right; k++)
|
for (int k = i; k <= right; k++)
|
||||||
{
|
{
|
||||||
if (a[k] > pivot)
|
if (a[k] > pivot)
|
||||||
return k;
|
return k;
|
||||||
}
|
}
|
||||||
return right+1;
|
return right + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int getSmall(int a[], int j, int left, int pivot)
|
int getSmall(int *a, int j, int left, int pivot)
|
||||||
{
|
{
|
||||||
for(int k = j; k >= left; k--)
|
for (int k = j; k >= left; k--)
|
||||||
{
|
{
|
||||||
if (a[k] < pivot)
|
if (a[k] < pivot)
|
||||||
return k;
|
return k;
|
||||||
@ -36,60 +36,62 @@ void swap(int *a, int *b)
|
|||||||
*b = t;
|
*b = t;
|
||||||
}
|
}
|
||||||
|
|
||||||
void random_quick(int a[], int left, int right)
|
void random_quick(int *a, int left, int right)
|
||||||
{
|
{
|
||||||
if (left>=right)
|
if (left >= right)
|
||||||
return;
|
return;
|
||||||
int index = left + (rand()%(right-left)), i = left, j = right;
|
int index = left + (rand() % (right - left)), i = left, j = right;
|
||||||
int pivot_index = index;
|
int pivot_index = index;
|
||||||
int pivot = a[index];
|
int pivot = a[index];
|
||||||
// storing index of element greater than pivot
|
// storing index of element greater than pivot
|
||||||
i = getBig(a, i, right, pivot);
|
i = getBig(a, i, right, pivot);
|
||||||
// storing index of element smaller than pivot
|
// storing index of element smaller than pivot
|
||||||
j = getSmall(a, j, left, pivot);
|
j = getSmall(a, j, left, pivot);
|
||||||
while(i <= j)
|
while (i <= j)
|
||||||
{
|
{
|
||||||
swap(&a[i], &a[j]);
|
swap(&a[i], &a[j]);
|
||||||
i = getBig(a, i, right, pivot);
|
i = getBig(a, i, right, pivot);
|
||||||
j = getSmall(a, j, left, pivot);
|
j = getSmall(a, j, left, pivot);
|
||||||
}
|
}
|
||||||
// after separating the smaller and greater elements, there are 3 cases possible
|
// after separating the smaller and greater elements, there are 3 cases possible
|
||||||
if(pivot_index>j && pivot_index>i)
|
if (pivot_index > j && pivot_index > i)
|
||||||
{
|
{
|
||||||
// case 1. When the pivot element index is greater than both i and j
|
// case 1. When the pivot element index is greater than both i and j
|
||||||
swap(&a[i], &a[pivot_index]);
|
swap(&a[i], &a[pivot_index]);
|
||||||
random_quick(a, left, i-1);
|
random_quick(a, left, i - 1);
|
||||||
random_quick(a, i+1, right);
|
random_quick(a, i + 1, right);
|
||||||
}
|
}
|
||||||
else if (pivot_index<j && pivot_index<i)
|
else if (pivot_index < j && pivot_index < i)
|
||||||
{
|
{
|
||||||
// case 2. When the pivot element index is smaller than both i and j
|
// case 2. When the pivot element index is smaller than both i and j
|
||||||
swap(&a[j], &a[pivot_index]);
|
swap(&a[j], &a[pivot_index]);
|
||||||
random_quick(a, left, j-1);
|
random_quick(a, left, j - 1);
|
||||||
random_quick(a, j+1, right);
|
random_quick(a, j + 1, right);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// the pivot element is at its origin position.
|
// the pivot element is at its origin position.
|
||||||
random_quick(a, left, pivot_index-1);
|
random_quick(a, left, pivot_index - 1);
|
||||||
random_quick(a, pivot_index+1, right);
|
random_quick(a, pivot_index + 1, right);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
srand(time(0));
|
srand(time(0));
|
||||||
int num;
|
int num;
|
||||||
scanf("%d", &num);
|
scanf("%d", &num);
|
||||||
int arr[num];
|
int *arr = (int *)malloc(num * sizeof(int));
|
||||||
for(int i = 0; i < num; i++)
|
for (int i = 0; i < num; i++)
|
||||||
{
|
{
|
||||||
scanf("%d", &arr[i]);
|
scanf("%d", &arr[i]);
|
||||||
}
|
}
|
||||||
random_quick(arr, 0, num-1);
|
random_quick(arr, 0, num - 1);
|
||||||
for(int i = 0; i < num; i++)
|
for (int i = 0; i < num; i++)
|
||||||
{
|
{
|
||||||
printf("%d ", arr[i]);
|
printf("%d ", arr[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
free(arr);
|
||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
|
@ -1,35 +1,41 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
void swap(int *a, int *b){
|
void swap(int *a, int *b)
|
||||||
|
{
|
||||||
int temp;
|
int temp;
|
||||||
temp = *a;
|
temp = *a;
|
||||||
*a = *b;
|
*a = *b;
|
||||||
*b = temp;
|
*b = temp;
|
||||||
}
|
}
|
||||||
void shakersort(int a[], int n)
|
void shakersort(int *a, int n)
|
||||||
{
|
{
|
||||||
int p, i;
|
int p, i;
|
||||||
for (p = 1; p <= n / 2; p++)
|
for (p = 1; p <= n / 2; p++)
|
||||||
{
|
{
|
||||||
for (i = p - 1; i < n - p; i++)
|
for (i = p - 1; i < n - p; i++)
|
||||||
if (a[i] > a[i+1]){
|
if (a[i] > a[i + 1])
|
||||||
|
{
|
||||||
swap(&a[i], &a[i + 1]);
|
swap(&a[i], &a[i + 1]);
|
||||||
}
|
}
|
||||||
for (i = n - p - 1; i >= p; i--)
|
for (i = n - p - 1; i >= p; i--)
|
||||||
if (a[i] < a[i-1]){
|
if (a[i] < a[i - 1])
|
||||||
|
{
|
||||||
swap(&a[i], &a[i - 1]);
|
swap(&a[i], &a[i - 1]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
int n;
|
int n;
|
||||||
scanf("%d",&n);
|
scanf("%d", &n);
|
||||||
int arr[n] ,i;
|
int *arr = (int *)malloc(n * sizeof(int));
|
||||||
for (i = 0 ; i < n; i++)
|
int i;
|
||||||
|
for (i = 0; i < n; i++)
|
||||||
scanf("%d ", &arr[i]);
|
scanf("%d ", &arr[i]);
|
||||||
shakersort(arr, n);
|
shakersort(arr, n);
|
||||||
for (i = 0 ; i < n; i++)
|
for (i = 0; i < n; i++)
|
||||||
printf("%d ", arr[i]);
|
printf("%d ", arr[i]);
|
||||||
|
free(arr);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user