bubble sort matrix with different col length - c

i need to do a bubble sort to matrix with different col lengths,
it need to be without the [] operand and in the language C.
i tried to do it like that :
for (i = 0; i < 5; i++)
{
first = (arr + i+d);// the first value in every line
for (j = 1; j <= (*first); j++)
{
for (d = 0; d <*(first)- j - 1; d++)
{
if (*(first+d+i) > *(first+d+1+i) )
{
swap =*(first+d);
*(first+d) = *(first+d+1);
*(first+d+1) = swap;
}
}
}
}
but it didn't work, someone can help me please ?

#include <stdio.h>
#include <stdlib.h>
typedef int Type;
#define PRN_Type "%d"
void print(Type **a, int size){
for(int i = 0;i<size;++i){
for(int j=1;j<=a[i][0];++j){
printf(PRN_Type " ", a[i][j]);
}
printf("\n");
}
}
void b_sort(Type **arr, int size){
int i, j, d;
Type swap, *first;
for (i = 0; i < size; ++i){
first = arr[i];//!!
for (j = 1; j <= *first; ++j){
for (d = 2; d <= *first -j+1; ++d){
if (*(first+d-1) > *(first+d) ){
swap =*(first+d-1);
*(first+d-1) = *(first+d);
*(first+d) = swap;
}
}
}
}
}
int main(void){
/*
int data[5][] = {
{3, 2,4,1},
{2, 99, 55},
{5, 9,5,1,7,5},
{1, 100},
{4, 5,5,5,5}
}
*/
int size = 5;
Type **data;
data = malloc(size * sizeof(Type*));
data[0]=(Type[]){3, 2,4,1};
data[1]=(Type[]){2, 99, 55};
data[2]=(Type[]){5, 9,5,1,7,5};
data[3]=(Type[]){1, 100};
data[4]=(Type[]){4, 5,5,5,5};
print(data, size);
b_sort(data, size);
printf("\n");
print(data, size);
return 0;
}

Related

A program sort array by remainder 3

I was requested to write an effecient function with running time n which sort array by the remainder of 3 the program puts the elements which the remainder from dividing in 3 is 0 afterwards the elements that the remainder is 1 and afterwards 2
for example the array {7, 16, 3, 28, 12, 31, 14, 12}
will be sortes that way {12, 3, 12, 28, 16, 31, 7, 14}
so I tries to write an efficient function but it have not cover all cases and does not works for all arrays
int arr[] = { 7,16,3,28,12,31,14,12 };
int rem0 = 0, rem1 = 1, rem2 = 2;
for (int i = 0; i < 8; i++) {
if (arr[i] % 3 == 0)
rem0++;
if (arr[i] % 3 == 1)
rem1++;
if (arr[i] % 3 == 2)
rem2++;
}
int k = rem0, p = 0, m = 0 = 0;
for (int i = 0; i < 8; i++) {
while (rem0-k){
swap(&arr[i], &arr[rem0 - k]);
k--;
}
if (arr[i] % 3 == 1 && rem0+m<7) {
swap(&arr[i], &arr[rem0 + m]);
m++;
}
if (arr[i] % 3 == 1 && rem0 + rem1 + p<7) {
swap(&arr[i], &arr[rem0+rem1 + p]);
p++;
}
}
for (int l = 0;l <8;l++) {
printf("%d\n", arr[l]);
}
}
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
swap switch elements,
Can anyone tells me how can I fix that?
thanks:)
Since you want your function to run in O(n) time, you cannot sort the array completely. All you need to do is put all the elements in 3 buckets.
The following algorithm runs in 2 phases.
//First we count the number of elements in each bucket
int count[3] ={0, 0, 0};
for (int i = 0; i < NUM_ELEMENTS; i++) {
count[arr[i]%3]++;
}
Now that we have the number of elements, we can calculate the offsets of each bucket and create and output array
int output[NUM_ELEMENTS]; // In place bucketing can also be done using swaps
count[2] = count[0] + count[1];
count[1] = count[0];
count[0] = 0;
for (int i = 0; i < NUM_ELEMENTS; i++) {
output[count[arr[i]%3]] = arr[i];
count[arr[i]%3]++;
}
// Finally print the array
for (int i = 0; i < NUM_ELEMENTS; i++) {
printf("%d", output[i]);
}
Demo on Ideone
Here is the solution which you are looking for which uses the same array:
#include <stdio.h>
#define REMINDER 3
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
int main()
{
int arr[] = {1,2,3,4,5,6,7,8,9,0};
int arr_size = sizeof(arr)/sizeof(arr[0]);
int idx=0;
for (int r=0; r<REMINDER; r++) {
for (int i=0; i<arr_size; i++) {
if (arr[i]%REMINDER==r) {
swap(&arr[idx++], &arr[i]);
}
}
}
for (int i=0; i<arr_size; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Here is a another solution which is just simpler by using other place to store the result:
#include <stdio.h>
#define REMINDER 3
#define ARR_SIZE 10
int main()
{
int arr[ARR_SIZE] = {1,2,3,4,5,6,7,8,9,0};
int arr_sorted[ARR_SIZE];
int idx=0;
for (int r=0; r<REMINDER; r++) {
for (int i=0; i<ARR_SIZE; i++) {
if (arr[i]%REMINDER==r) {
arr_sorted[idx++]=arr[i];
}
}
}
for (int i=0; i<ARR_SIZE; i++) {
printf("%d ", arr_sorted[i]);
}
return 0;
}
Here's a 1-pass in-place Dutch national flag algorithm implementation (thanks to #Virgile who pointed out the algorithm)
void swap(int *a, int *b) {
int tmp = *a;
*a = *b;
*b = tmp;
}
// Dutch National Flag (see xlinux.nist.gov/dads/HTML/DutchNationalFlag.html)
void sort3dnf(int *a, size_t n) {
int *bot = a;
int *mid = a;
int *top = a + n - 1;
while (mid <= top) {
switch (*mid % 3) {
default: swap(bot++, mid++); break;
case 1: mid++; break;
case 2: swap(mid, top--); break;
}
}
}
See ideone.com/6QXXCN
hey thanks for the advice
sadly we had requested to write the code
without any added array
I will be very glad if you could help me to
solve the issue
thanks :)
hey thanks for the advice sadly we had requested to write the code without any added array I will be very glad if you could help me to solve the issue thanks :)
Here is the answer without adding any extra array:
#include <stdio.h>
#define REMINDER 3
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
int main()
{
int arr[] = {1,2,3,4,5,6,7,8,9,0};
int arr_size = sizeof(arr)/sizeof(arr[0]);
int idx=0;
for (int r=0; r<REMINDER; r++) {
for (int i=0; i<arr_size; i++) {
if (arr[i]%REMINDER==r) {
swap(&arr[idx++], &arr[i]);
}
}
}
for (int i=0; i<arr_size; i++) {
printf("%d ", arr[i]);
}
return 0;
}

Rotating the array using C

Why this code is giving segmentation fault error?
This is code using for loop for left shift the given array at stdin by no of rotations.
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int *rotatebyone(int *array_input, int len_of_arr);
void rotate(int *array_input, int len_of_arr, int no_of_rota);
int main()
{
int array1[] = { };
int array2[] = { };
int array3[] = { };
int i = 0, j = 0, size_of_arr = 0, no_of_rotations = 0;
for (i = 0; i < 2; i++)
{
scanf("%d", &array1[i]);
//printf("%d", array1[i]);
}
size_of_arr = array1[0];
no_of_rotations = array1[1];
for (i = 0; i < size_of_arr; i++)
{
scanf("%d", &array2[i]);
//printf("%d ", array2[i]);
}
rotate(array2, size_of_arr, no_of_rotations);
return 0;
}
void rotate(int *array_input, int size_of_array, int no_of_rota)
{
int h = 0;
for (h = 0; h < no_of_rota; h++)
{
rotatebyone(array_input, size_of_array);
}
for (h = 0; h < size_of_array; h++)
printf("%d ", array_input[h]);
}
int *rotatebyone(int *array_input1, int len_of_arr)
{
//int array3 = {};
int j = 0, k = 0;
int temp = 0;
temp = array_input1[0];
for (k = 0; k < len_of_arr - 1; k++)
{
array_input1[k] = array_input1[k + 1];
}
array_input1[len_of_arr - 1] = temp;
return array_input1;
}
For array2[], you have not mentioned its size. It can be evn 1000 or 10000. Better use int * array2 and allot memory through malloc.
Reg array1, it can be initialesed as int array1[2] since we know two elements are there for array1.

How to use second rank pointer rightly?

I have a 2D array called matrix, now I would like to add one to every element.
#include <stdio.h>
#include <stdlib.h>
int **add_one(int **matrix, int m, int n) {
int i, j;
int **new_mat;
new_mat = (int **) malloc(m * n *sizeof(int));
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
new_mat[i][j] = matrix[i][j] + 1;
}
}
//return the 2nd rank pointer
return new_mat;
}
int main() {
int matrix[3][2] = {1, 2, 3, 4, 5, 6};
int **new_mat;
int i, j;
new_mat = add_one(matrix, 3, 2);
for (i = 0; i < 3; i++) {
for (j = 0; j < 2; j++) {
printf("%d ", new_mat[i][j]);
}
printf("\n");
}
free(new_mat);
}
However, the compiler told me that
[Error] cannot convert 'int (*)[2]' to 'int**' for argument '1' to 'int** add_one(int**, int, int)'
It works a bit differently from what you thought
#include <stdio.h>
#include <stdlib.h>
// you can only pass what you have
int **add_one(int matrix[3][2], int m, int n)
{
int i, j;
int **new_mat;
// allocate m pointers (the rows)
new_mat = malloc(m * sizeof(*new_mat));
for (i = 0; i < m; i++) {
// allocate space for n ints
new_mat[i] = malloc(n * sizeof(int));
for (j = 0; j < n; j++) {
new_mat[i][j] = matrix[i][j] + 1;
}
}
//return the 2nd rank pointer
return new_mat;
}
int main()
{
// you forgot to encapsulate the rows, too
int matrix[3][2] = { {1, 2}, {3, 4}, {5, 6} };
int **new_mat;
int i, j;
new_mat = add_one(matrix, 3, 2);
for (i = 0; i < 3; i++) {
for (j = 0; j < 2; j++) {
printf(" %d ", new_mat[i][j]);
}
printf("\n");
}
free(new_mat);
}
If you want to pass the two-star matrix as a two-star matrix you need to build it like the new_mat in the function add_one.

Sorting integer array to avoid repeating consecutive values in C

I'm using Linux to implement this sorting. If I have a array arr[] ={1, 1, 1, 2, 2, 3, 3}, how to sort it by this:
arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 1
arr[4] = 2
arr[5] = 3
arr[6] = 1
I Tried to do something like this:
for (int i = 0; i < size; i++)
{
if (arr[i] < arr[i+1])
{
}
}
Please give me some suggestions, thank you so much!
a hint
first you have to sort the array, then starting from 1 to (end-1) if active item equals to the last item move it to the end.
#include <stdio.h>
#include <stdlib.h>
/*__________________________________________________
*/
static int __cdecl sortCallback(int *i1,int *i2){
return (*i1<*i2)?-1:(*i1>*i2)?1:0;
}
/*__________________________________________________
*/
void printArr(char* Title,int *arr,int n){
int i;
if(Title)
printf("%s:\n\t",Title);
for (i=0;i<n;i++)
printf("%d ",arr[i]);
printf("\n");
return;
}
/*__________________________________________________
*/
void arrange(int *arr,int n){
int i=1,j;
int a;
while(i<(n-1)){
if(arr[i]==arr[i-1]){
a=arr[i];
for(j=i;j<(n+1);j++){
arr[j]=arr[j+1];
}
arr[n-1]=a;
}else
i++;
}
}
/*__________________________________________________
*/
int main(void){
int arr[7];
arr[0] = 1;
arr[1] = 2;
arr[2] = 3;
arr[3] = 1;
arr[4] = 2;
arr[5] = 3;
arr[6] = 1;
printArr("Initial",arr,7);
qsort(arr,7,sizeof(int),sortCallback);
printArr("Sorted",arr,7);
arrange(arr,7);
printArr("Rearranged",arr,7);
return 0;
}
#include <stdio.h>
void cnv(int n, int arr[n]){//arr is sorted
struct {
int value;
int occurs;
} tmp[n];
//make distinct array
int k = 0;
tmp[k].value = arr[0];
tmp[k].occurs = 1;
for(int i = 1; i < n; ++i){
if(arr[i] != arr[i-1]){
tmp[++k].value = arr[i];
tmp[k].occurs = 1;
} else {
++tmp[k].occurs;
}
}
//Written back
for(int i = 0, j = 0; i < n; ++i){
while(tmp[j].occurs == 0){
j = (j == k) ? 0 : j + 1;
}
arr[i] = tmp[j].value;
--tmp[j].occurs;
j = (j == k) ? 0 : j + 1;
}
}
int main(void){
int arr[] ={1, 1, 1, 2, 2, 3, 3};
int n = sizeof(arr)/sizeof(*arr);
cnv(n, arr);
for(int i = 0; i < n; ++i) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}

How to write all combination with given array

I have some array with numbers. When I want to write all combination with 3 digits I wrote this. But now I need edit this code to return all combination with 1 to numbers.size digits. How should I edit it ?
int items[] = {1, 2, 3, 4, 5};
int itemSize = 5;
for (int i = 0; i < itemSize - 2; i++) {
for (int j = i + 1; j < itemSize - 1; j++) {
for (int k = j + 1; k < itemSize; k++)
printf("%d%d%d\n", items[i], items[j], items[k]);
}
}
it is not perfect solution, but it is well enough to work. you can get all sub array from an array, from 1 element to length array
void permute(char* previous, char *a, int i, int n,int nmax)
{
int j;
if (i == n)
{
char c = a[nmax];
a[nmax] = 0;
if (strstr(previous,a) == 0)
{
printf("%s\n", a);
strncpy(previous,a,n);
}
a[nmax] = c;
}
else
{
for (j = i; j < n; j++)
{
char c = a[i];
a[i] = a[j];
a[j] = c;
permute(previous,a, i+1, n,nmax);
c = a[i];
a[i] = a[j];
a[j] = c;
}
}
}
void subarrays(char *a, int len)
{
int i=1;
char *previous = strdup(a);
*previous = 0;
for(i=1;i<len+1;i++)
{
permute(previous,a,0,len,i);
}
}
int main(void) {
char *arr = strdup("abcde");
subarrays(arr,strlen(arr));
return EXIT_SUCCESS;
}
Since you're already doing a web search for this, here is a generic solution.
#include <stdio.h>
int next_combination(size_t *I, size_t k, size_t n)
{
size_t i, j;
i = k-1; /* find next element to increment */
while(I[i] == (n-k+i)){
--i;
if(i == (size_t)-1){ /* if done */
for(i = 0; i < k; i++) /* return with initial combination */
I[i] = i;
return(0);
}
}
I[i] += 1; /* increment element */
for(j = i+1; j < k; j++) /* create increasing string */
I[j] = I[i]+j-i;
return(1); /* return with new combination */
}
int main(int argc, char **argv)
{
int A[5] = {1, 2, 3, 4, 5};
size_t I[5];
size_t i, k, n;
n = sizeof(A)/sizeof(A[0]); /* n things */
for(k = 1; k <= n; k++){ /* n things k at a time */
for(i = 0; i < k; i++) /* create initial combination */
I[i] = i;
do{ /* display combinations */
for(i = 0; i < k; i++)
printf("%2d", A[I[i]]);
printf("\n");
}
while(next_combination(I, k, n));
}
return(0);
}

Resources