mirror of
https://github.com/TheAlgorithms/C
synced 2024-11-25 06:49:36 +03: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)
|
||||
add_compile_definitions(_CRT_SECURE_NO_WARNINGS)
|
||||
add_compile_options(/Za)
|
||||
endif(MSVC)
|
||||
|
||||
add_subdirectory(conversions)
|
||||
|
@ -3,13 +3,17 @@
|
||||
//generate notes for an entered amount N.
|
||||
|
||||
#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) return 1;
|
||||
if(k == 0) return 0;
|
||||
return ways(n, a, k-1) + ways(n-a[k-1], a, k);
|
||||
if (n < 0 || k < 0)
|
||||
return 0;
|
||||
if (n == 0)
|
||||
return 1;
|
||||
if (k == 0)
|
||||
return 0;
|
||||
return ways(n, a, k - 1) + ways(n - a[k - 1], a, k);
|
||||
}
|
||||
|
||||
int main()
|
||||
@ -20,15 +24,15 @@ int main()
|
||||
|
||||
printf("Number of coins? ");
|
||||
scanf("%d", &m);
|
||||
int coin[m], i;
|
||||
for(i=0; i<m; i++)
|
||||
int *coin = (int *)malloc(m * sizeof(int)), i;
|
||||
for (i = 0; i < m; i++)
|
||||
{
|
||||
printf("coin? ");
|
||||
scanf("%d", &coin[i]);
|
||||
}
|
||||
|
||||
printf("---- your requests --- \n");
|
||||
while(1)
|
||||
while (1)
|
||||
{
|
||||
printf("amount? exit(0) ");
|
||||
scanf("%d", &n);
|
||||
@ -38,5 +42,7 @@ int main()
|
||||
}
|
||||
printf("%d\n", ways(n, coin, m));
|
||||
}
|
||||
|
||||
free(coin);
|
||||
return 0;
|
||||
}
|
||||
|
@ -2,52 +2,61 @@
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void swap(char* left, char* right){
|
||||
void swap(char *left, char *right)
|
||||
{
|
||||
char temp = *left;
|
||||
*left = *right;
|
||||
*right = temp;
|
||||
}
|
||||
|
||||
int compare (const void * a, const void * b){
|
||||
return ( *(char*)a - *(char*)b );
|
||||
int compare(const void *a, const void *b)
|
||||
{
|
||||
return (*(char *)a - *(char *)b);
|
||||
}
|
||||
|
||||
void PrintSortedPermutations(char str[])
|
||||
void PrintSortedPermutations(char *str)
|
||||
{
|
||||
int strSize = strlen(str);
|
||||
qsort(str, strSize, sizeof(char), compare);
|
||||
|
||||
int largerPermFound = 1;
|
||||
do{
|
||||
do
|
||||
{
|
||||
// 1. Print permutation
|
||||
printf("%s\n", str);
|
||||
// 2. Find rightmost char that is smaller than char to its right
|
||||
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 (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
|
||||
int j = i+1, k;
|
||||
for(k=j; k<strSize && str[k]; k++){
|
||||
int j = i + 1, k;
|
||||
for (k = j; k < strSize && str[k]; k++)
|
||||
{
|
||||
if (str[k] > str[i] && str[k] < str[j])
|
||||
j = k;
|
||||
}
|
||||
// 3. Swap chars at i and j
|
||||
swap(&str[i], &str[j]);
|
||||
// 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;
|
||||
}
|
||||
while(largerPermFound);
|
||||
else
|
||||
largerPermFound = 0;
|
||||
} while (largerPermFound);
|
||||
}
|
||||
|
||||
int main() {
|
||||
int main()
|
||||
{
|
||||
int n; //size of string
|
||||
scanf("%d\n",&n);
|
||||
char str[n];
|
||||
scanf("%s",str);
|
||||
scanf("%d\n", &n);
|
||||
char *str = (char *)malloc(n * sizeof(char));
|
||||
scanf("%s", str);
|
||||
PrintSortedPermutations(str);
|
||||
free(str);
|
||||
return 0;
|
||||
}
|
||||
|
@ -8,70 +8,86 @@
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
const int M=144;
|
||||
#define M 144
|
||||
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;
|
||||
for(j=0; j<N; j++)
|
||||
if(a[x*N+j] == v)
|
||||
for (j = 0; j < N; j++)
|
||||
if (a[x * N + j] == v)
|
||||
return 0;
|
||||
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;
|
||||
for(i=0; i<N; i++)
|
||||
if(a[i*N+y] == v)
|
||||
for (i = 0; i < N; i++)
|
||||
if (a[i * N + y] == v)
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
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++)
|
||||
for(j=0; j<C; j++)
|
||||
if(a[(i + bi*R)*N + (j + bj*C)] == 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++)
|
||||
for (j = 0; j < C; j++)
|
||||
if (a[(i + bi * R) * N + (j + bj * C)] == v)
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
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);
|
||||
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);
|
||||
}
|
||||
|
||||
void print(int a[M]) {
|
||||
void print(int a[M])
|
||||
{
|
||||
int i, j;
|
||||
for(i=0; i<N; i++)
|
||||
for(j=0; j<N; j++)
|
||||
printf("%d%c", a[i*N+j], (j==N-1 ? '\n':' '));
|
||||
for (i = 0; i < N; i++)
|
||||
for (j = 0; j < N; j++)
|
||||
printf("%d%c", a[i * N + j], (j == N - 1 ? '\n' : ' '));
|
||||
}
|
||||
|
||||
int solve(int a[M]) {
|
||||
int i, j, v, rem=0;
|
||||
for(i=0; i<N; i++) {
|
||||
for(j=0; j<N; j++) {
|
||||
if(a[i*N+j] == 0) {
|
||||
int solve(int a[M])
|
||||
{
|
||||
int i, j, v, rem = 0;
|
||||
for (i = 0; i < N; i++)
|
||||
{
|
||||
for (j = 0; j < N; j++)
|
||||
{
|
||||
if (a[i * N + j] == 0)
|
||||
{
|
||||
rem = 1;
|
||||
for(v=1; v<=N; v++) {
|
||||
if(OK(a,i,j,v)) {
|
||||
a[i*N+j] = v;
|
||||
if(solve(a)) return 1;
|
||||
a[i*N+j] = 0;
|
||||
for (v = 1; v <= N; v++)
|
||||
{
|
||||
if (OK(a, i, j, v))
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int main()
|
||||
{
|
||||
scanf("%d%d%d", &N, &R, &C);
|
||||
int a[M], i, j;
|
||||
for(i=0; i<N; i++)
|
||||
for(j=0; j<N; j++)
|
||||
scanf("%d", &a[i*N+j]);
|
||||
for (i = 0; i < N; i++)
|
||||
for (j = 0; j < N; j++)
|
||||
scanf("%d", &a[i * N + j]);
|
||||
|
||||
if(solve(a)) print(a);
|
||||
else printf("Invalid\n");
|
||||
if (solve(a))
|
||||
print(a);
|
||||
else
|
||||
printf("Invalid\n");
|
||||
return 0;
|
||||
}
|
||||
|
@ -22,7 +22,7 @@
|
||||
/**
|
||||
* 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.;
|
||||
unsigned int n;
|
||||
@ -154,7 +154,7 @@ int main(int argc, char **argv)
|
||||
|
||||
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;
|
||||
for (i = 0; i < degree - 1; i++)
|
||||
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.
|
||||
void binarySearch(int mat[n][m], int i, int j_low,int j_high, int x)
|
||||
// This function does Binary search for x in i-th row from j_low to j_high.
|
||||
void binarySearch(int **mat, int i, int j_low, int j_high, int x)
|
||||
{
|
||||
while (j_low <= j_high)
|
||||
{
|
||||
int j_mid = (j_low + j_high) / 2;
|
||||
|
||||
|
||||
// Element found
|
||||
if (mat[i][j_mid] == x){
|
||||
printf("Found at (%d,%d)\n",i,j_mid);
|
||||
return ;
|
||||
if (mat[i][j_mid] == x)
|
||||
{
|
||||
printf("Found at (%d,%d)\n", i, j_mid);
|
||||
return;
|
||||
}
|
||||
else if (mat[i][j_mid] > x)
|
||||
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
|
||||
printf("element not found\n");
|
||||
}
|
||||
|
||||
|
||||
// Function to perform binary search on the mid values of row to get the desired pair of rows
|
||||
// where the element can be found
|
||||
void modifiedBinarySearch(int mat[n][m], int n, int m, int x)
|
||||
{ // If Single row matrix
|
||||
if (n == 1){
|
||||
binarySearch(mat, 0, 0, m-1, x);
|
||||
void modifiedBinarySearch(int **mat, int n, int m, int x)
|
||||
{ // If Single row matrix
|
||||
if (n == 1)
|
||||
{
|
||||
binarySearch(mat, 0, 0, m - 1, x);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// Do binary search in middle column.
|
||||
// Condition to terminate the loop when the 2 desired rows are found.
|
||||
int i_low = 0, i_high = n-1, j_mid = m/2;
|
||||
while ((i_low+1) < i_high)
|
||||
int i_low = 0, i_high = n - 1, j_mid = m / 2;
|
||||
while ((i_low + 1) < i_high)
|
||||
{
|
||||
int i_mid = (i_low + i_high) / 2;
|
||||
// element found
|
||||
if (mat[i_mid][j_mid] == x){
|
||||
printf("Found at (%d,%d)\n",i_mid,j_mid);
|
||||
if (mat[i_mid][j_mid] == x)
|
||||
{
|
||||
printf("Found at (%d,%d)\n", i_mid, j_mid);
|
||||
return;
|
||||
}
|
||||
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 (mat[i_low][j_mid] == x)
|
||||
printf("Found at (%d,%d)\n",i_low,j_mid);
|
||||
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, j_mid);
|
||||
else if (mat[i_low + 1][j_mid] == x)
|
||||
printf("Found at (%d,%d)\n", i_low + 1, j_mid);
|
||||
|
||||
// Search element on 1st half of 1st row
|
||||
else if (x <= mat[i_low][j_mid-1])
|
||||
binarySearch(mat, i_low, 0, j_mid-1, x);
|
||||
|
||||
else if (x <= mat[i_low][j_mid - 1])
|
||||
binarySearch(mat, i_low, 0, j_mid - 1, x);
|
||||
|
||||
// Search element on 2nd half of 1st row
|
||||
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);
|
||||
|
||||
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);
|
||||
|
||||
// Search element on 1st half of 2nd row
|
||||
else if (x <= mat[i_low+1][j_mid-1])
|
||||
binarySearch(mat, i_low+1, 0, j_mid-1, x);
|
||||
|
||||
else if (x <= mat[i_low + 1][j_mid - 1])
|
||||
binarySearch(mat, i_low + 1, 0, j_mid - 1, x);
|
||||
|
||||
// search element on 2nd half of 2nd row
|
||||
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 x; //element to be searched
|
||||
scanf("%d %d %d\n",&n,&m,&x);
|
||||
int mat[n][m];
|
||||
for(int i=0; i<n; i++){
|
||||
for(int j=0; j<m; j++){
|
||||
scanf("%d",&mat[i][j]);
|
||||
int x; //element to be searched
|
||||
scanf("%d %d %d\n", &n, &m, &x);
|
||||
int **mat = (int **)malloc(n * sizeof(int *));
|
||||
for (x = 0; x < n; x++)
|
||||
mat[x] = (int *)malloc(m * sizeof(int));
|
||||
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
for (int j = 0; j < m; j++)
|
||||
{
|
||||
scanf("%d", &mat[i][j]);
|
||||
}
|
||||
}
|
||||
modifiedBinarySearch(mat, n, m, x);
|
||||
|
||||
for (x = 0; x < n; x++)
|
||||
free(mat[x]);
|
||||
free(mat);
|
||||
return 0;
|
||||
}
|
||||
|
@ -1,37 +1,43 @@
|
||||
//sorting of array list using selection sort
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/*Displays the array, passed to this method*/
|
||||
void display(int arr[], int n){
|
||||
|
||||
void display(int *arr, int n)
|
||||
{
|
||||
|
||||
int i;
|
||||
for(i = 0; i < n; i++){
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
printf("%d ", arr[i]);
|
||||
}
|
||||
|
||||
|
||||
printf("\n");
|
||||
|
||||
}
|
||||
|
||||
/*Swap function to swap two values*/
|
||||
void swap(int *first, int *second){
|
||||
|
||||
void swap(int *first, int *second)
|
||||
{
|
||||
|
||||
int temp = *first;
|
||||
*first = *second;
|
||||
*second = temp;
|
||||
|
||||
}
|
||||
|
||||
/*This is where the sorting of the array takes place
|
||||
arr[] --- Array to be sorted
|
||||
size --- Array Size
|
||||
*/
|
||||
void selectionSort(int arr[], int size){
|
||||
|
||||
for(int i=0; i<size; i++) {
|
||||
void selectionSort(int *arr, int size)
|
||||
{
|
||||
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
int min_index = i;
|
||||
for(int j=i+1; j<size; j++) {
|
||||
if(arr[min_index] > arr[j]) {
|
||||
for (int j = i + 1; j < size; j++)
|
||||
{
|
||||
if (arr[min_index] > arr[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;
|
||||
printf("Enter size of array:\n");
|
||||
scanf("%d", &n); // E.g. 8
|
||||
|
||||
|
||||
printf("Enter the elements of the array\n");
|
||||
int i;
|
||||
int arr[n];
|
||||
for(i = 0; i < n; i++){
|
||||
scanf("%d", &arr[i] );
|
||||
int *arr = (int *)malloc(n * sizeof(int));
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
scanf("%d", &arr[i]);
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -2,42 +2,48 @@
|
||||
* Using binary search to find the proper location for
|
||||
* inserting the selected item at each iteration. */
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/*Displays the array, passed to this method*/
|
||||
void display(int arr[], int n) {
|
||||
void display(int *arr, int n)
|
||||
{
|
||||
int i;
|
||||
for(i = 0; i < n; i++){
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
printf("%d ", arr[i]);
|
||||
}
|
||||
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)
|
||||
return (key > arr[low]) ? (low + 1): low;
|
||||
return (key > arr[low]) ? (low + 1) : low;
|
||||
int mid = low + (high - 1) / 2;
|
||||
if(arr[mid] == key)
|
||||
if (arr[mid] == key)
|
||||
return mid + 1;
|
||||
else if (arr[mid] > key)
|
||||
return binarySearch(arr, key, low, mid - 1);
|
||||
else
|
||||
return binarySearch(arr, key, mid + 1, high);
|
||||
|
||||
}
|
||||
/*This is where the sorting of the array takes place
|
||||
arr[] --- Array to be sorted
|
||||
size --- Array Size
|
||||
*/
|
||||
void insertionSort(int arr[], int size) {
|
||||
void insertionSort(int *arr, int size)
|
||||
{
|
||||
int i, j, key, index;
|
||||
for(i = 0; i < size; i++) {
|
||||
for (i = 0; i < size; i++)
|
||||
{
|
||||
j = i - 1;
|
||||
key = arr[i];
|
||||
/* Use binrary search to find exact key's index */
|
||||
index = binarySearch(arr, key, 0, j);
|
||||
/* Move all elements greater than key from [index...j]
|
||||
* to one position */
|
||||
while(j >= index) {
|
||||
while (j >= index)
|
||||
{
|
||||
arr[j + 1] = arr[j];
|
||||
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;
|
||||
printf("Enter size of array:\n");
|
||||
scanf("%d", &n); // E.g. 8
|
||||
|
||||
printf("Enter the elements of the array\n");
|
||||
int i;
|
||||
int arr[n];
|
||||
for(i = 0; i < n; i++) {
|
||||
scanf("%d", &arr[i] );
|
||||
int *arr = (int *)malloc(n * sizeof(int));
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
scanf("%d", &arr[i]);
|
||||
}
|
||||
|
||||
printf("Original array: ");
|
||||
@ -66,6 +74,6 @@ int main(int argc, const char * argv[]) {
|
||||
printf("Sorted array: ");
|
||||
display(arr, n);
|
||||
|
||||
free(arr);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -7,6 +7,7 @@
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
@ -15,26 +16,26 @@ int main()
|
||||
printf("Enter size of array = ");
|
||||
scanf("%d", &n);
|
||||
|
||||
int a[n];
|
||||
int *a = (int *)malloc(n * sizeof(int));
|
||||
printf("Enter %d elements in array :\n", n);
|
||||
for(i = 0; i < n; i++)
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
scanf("%d", &a[i]);
|
||||
if(a[i] > l)
|
||||
if (a[i] > l)
|
||||
l = a[i];
|
||||
}
|
||||
|
||||
int b[l + 1];
|
||||
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
|
||||
|
||||
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);
|
||||
b[i]--;
|
||||
@ -42,5 +43,6 @@ int main()
|
||||
}
|
||||
}
|
||||
|
||||
free(a);
|
||||
return 0;
|
||||
}
|
||||
|
@ -1,10 +1,13 @@
|
||||
//sorting of array list using insertion sort
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/*Displays the array, passed to this method*/
|
||||
void display(int arr[], int n) {
|
||||
void display(int *arr, int n)
|
||||
{
|
||||
int i;
|
||||
for(i = 0; i < n; i++){
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
printf("%d ", arr[i]);
|
||||
}
|
||||
printf("\n");
|
||||
@ -14,13 +17,16 @@ void display(int arr[], int n) {
|
||||
arr[] --- Array to be sorted
|
||||
size --- Array Size
|
||||
*/
|
||||
void insertionSort(int arr[], int size) {
|
||||
void insertionSort(int *arr, int size)
|
||||
{
|
||||
int i, j, key;
|
||||
for(i = 0; i < size; i++) {
|
||||
for (i = 0; i < size; i++)
|
||||
{
|
||||
j = i - 1;
|
||||
key = arr[i];
|
||||
/* 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];
|
||||
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;
|
||||
printf("Enter size of array:\n");
|
||||
scanf("%d", &n); // E.g. 8
|
||||
|
||||
printf("Enter the elements of the array\n");
|
||||
int i;
|
||||
int arr[n];
|
||||
for(i = 0; i < n; i++) {
|
||||
scanf("%d", &arr[i] );
|
||||
int *arr = (int *)malloc(n * sizeof(int));
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
scanf("%d", &arr[i]);
|
||||
}
|
||||
|
||||
printf("Original array: ");
|
||||
@ -49,6 +57,6 @@ int main(int argc, const char * argv[]) {
|
||||
printf("Sorted array: ");
|
||||
display(arr, n);
|
||||
|
||||
free(arr);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -1,10 +1,11 @@
|
||||
//sorting of array list using Radix sort
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define range 10 // Range for integers is 10 as digits range from 0-9
|
||||
|
||||
// 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];
|
||||
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
|
||||
void countSort(int arr[], int n, int place)
|
||||
void countSort(int *arr, int n, int place)
|
||||
{
|
||||
int i, freq[range] = {0};
|
||||
int output[n];
|
||||
@ -46,7 +47,7 @@ void countSort(int arr[], int n, int place)
|
||||
n --- Array Size
|
||||
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;
|
||||
while (max)
|
||||
@ -72,7 +73,7 @@ int main(int argc, const char *argv[])
|
||||
|
||||
printf("Enter the elements of the array\n");
|
||||
int i;
|
||||
int arr[n];
|
||||
int *arr = (int *)malloc(n * sizeof(int));
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
scanf("%d", &arr[i]);
|
||||
@ -84,10 +85,11 @@ int main(int argc, const char *argv[])
|
||||
int max;
|
||||
max = MAX(arr, n);
|
||||
|
||||
radixsort(arr, n, max);
|
||||
radixsort2(arr, n, max);
|
||||
|
||||
printf("Sorted array: ");
|
||||
display(arr, n); // Sorted array : 3 4 7 8 8 9 10 11
|
||||
|
||||
free(arr);
|
||||
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.
|
||||
The expected running time of the algorithm is O(nlog(n)).
|
||||
*/
|
||||
#include<stdio.h>
|
||||
#include<stdlib.h>
|
||||
#include<time.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.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)
|
||||
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)
|
||||
return k;
|
||||
@ -36,60 +36,62 @@ void swap(int *a, int *b)
|
||||
*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;
|
||||
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 = a[index];
|
||||
// storing index of element greater than pivot
|
||||
i = getBig(a, i, right, pivot);
|
||||
// storing index of element smaller than pivot
|
||||
j = getSmall(a, j, left, pivot);
|
||||
while(i <= j)
|
||||
while (i <= j)
|
||||
{
|
||||
swap(&a[i], &a[j]);
|
||||
i = getBig(a, i, right, pivot);
|
||||
j = getSmall(a, j, left, pivot);
|
||||
}
|
||||
// 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
|
||||
swap(&a[i], &a[pivot_index]);
|
||||
random_quick(a, left, i-1);
|
||||
random_quick(a, i+1, right);
|
||||
random_quick(a, left, i - 1);
|
||||
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
|
||||
swap(&a[j], &a[pivot_index]);
|
||||
random_quick(a, left, j-1);
|
||||
random_quick(a, j+1, right);
|
||||
random_quick(a, left, j - 1);
|
||||
random_quick(a, j + 1, right);
|
||||
}
|
||||
else
|
||||
{
|
||||
// the pivot element is at its origin position.
|
||||
random_quick(a, left, pivot_index-1);
|
||||
random_quick(a, pivot_index+1, right);
|
||||
random_quick(a, left, pivot_index - 1);
|
||||
random_quick(a, pivot_index + 1, right);
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
srand(time(0));
|
||||
srand(time(0));
|
||||
int num;
|
||||
scanf("%d", &num);
|
||||
int arr[num];
|
||||
for(int i = 0; i < num; i++)
|
||||
int *arr = (int *)malloc(num * sizeof(int));
|
||||
for (int i = 0; i < num; i++)
|
||||
{
|
||||
scanf("%d", &arr[i]);
|
||||
}
|
||||
random_quick(arr, 0, num-1);
|
||||
for(int i = 0; i < num; i++)
|
||||
random_quick(arr, 0, num - 1);
|
||||
for (int i = 0; i < num; i++)
|
||||
{
|
||||
printf("%d ", arr[i]);
|
||||
}
|
||||
|
||||
free(arr);
|
||||
printf("\n");
|
||||
}
|
||||
|
@ -1,35 +1,41 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void swap(int *a, int *b){
|
||||
void swap(int *a, int *b)
|
||||
{
|
||||
int temp;
|
||||
temp = *a;
|
||||
*a = *b;
|
||||
*b = temp;
|
||||
}
|
||||
void shakersort(int a[], int n)
|
||||
void shakersort(int *a, int n)
|
||||
{
|
||||
int p, i;
|
||||
for (p = 1; p <= n / 2; p++)
|
||||
{
|
||||
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]);
|
||||
}
|
||||
}
|
||||
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]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
scanf("%d",&n);
|
||||
int arr[n] ,i;
|
||||
for (i = 0 ; i < n; i++)
|
||||
scanf("%d", &n);
|
||||
int *arr = (int *)malloc(n * sizeof(int));
|
||||
int i;
|
||||
for (i = 0; i < n; i++)
|
||||
scanf("%d ", &arr[i]);
|
||||
shakersort(arr, n);
|
||||
for (i = 0 ; i < n; i++)
|
||||
for (i = 0; i < n; i++)
|
||||
printf("%d ", arr[i]);
|
||||
free(arr);
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user