Bubble sort function for strings causes program crashes
By : i'm Shahrul
Date : March 29 2020, 07:55 AM
will help you As noted in comments on the question, you need to allocate memory for the name elements. One easy way is to note that you hardcode the length of a name to 99 chars (the limit passed to fgets) and change your array to code :
char name[5][100];
if (strcmp(name[j] > name[j+1]))
if (strcmp(name[j], name[j+1]) > 0)
for (j=0 ; j<i; j++)
// < rather than <=

How can I bubble sort large numbers in an array?
By : Mohammad Javad Passl
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further My Question is , Firstly short the elements length wise code :
if (arr[i].length()>arr[j].length()) {
String temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
if(str[j].length() == (str[j+1]).length()){
if ( str[j].compareTo(str[j+1])>0 ){
String temp = str[j];
str[j] = str[j+1];
str[j+1] = temp;
}
}

Trying to write a C program to bubble sort 25 words from a 2 dimensional array alphabetically
By : user3177530
Date : March 29 2020, 07:55 AM
like below fixes the issue I figured out how to read in the strings, but I'm not sure how to use bubble sort to arrange them alphabetically. This is what I have to far. I keep receiving the error "Segmentation fault (core dumped). , Use strcmp code :
if(strcmp(Strings[i], Strings[i+1]) > 0)
{
//swap elements
}

Write a program that uses Bubble Sort to sort integers in a 2 dimensional array in ascending order
By : Mykey Kassa
Date : March 29 2020, 07:55 AM
may help you . You can cast pointer to the first row of a twodimensional array to pointer to int and sort the array as a onedimensional array. Here you are code :
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
void bubble_sort( int *a, size_t n )
{
for ( size_t last /* = n */; not ( n < 2 ); n = last )
{
for ( size_t i = last = 1; i < n; i++ )
{
if ( a[i] < a[i1] )
{
std::swap( a[i], a[i1] );
last = i;
}
}
}
}
int main()
{
const size_t N = 3;
const size_t M = 4;
int a[N][M];
std::srand( ( unsigned int )std::time( nullptr ) );
for ( size_t i = 0; i < N; i++ )
{
for ( size_t j = 0; j < M; j++ ) a[i][j] = std::rand() % ( M * N );
}
for ( size_t i = 0; i < N; i++ )
{
for ( size_t j = 0; j < M; j++ )
{
std::cout << std::setw( 2 ) << a[i][j] << ' ';
}
std::cout << std::endl;
}
std::cout << std::endl;
bubble_sort( reinterpret_cast<int *>( a ), N * M );
for ( size_t i = 0; i < N; i++ )
{
for ( size_t j = 0; j < M; j++ )
{
std::cout << std::setw( 2 ) << a[i][j] << ' ';
}
std::cout << std::endl;
}
return 0;
}
7 3 8 7
6 8 5 0
10 9 9 3
0 3 3 5
6 7 7 8
8 9 9 10
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
template <typename T, size_t N, size_t M>
void bubble_sort( T ( &a )[N][M] )
{
for ( size_t n = N * M, last /* = n */; not ( n < 2 ); n = last )
{
for ( size_t i = last = 1; i < n; i++ )
{
if ( a[i / M][i % M] < a[( i  1 ) / M][( i  1 ) % M] )
{
std::swap( a[i / M][i % M], a[( i  1 ) / M][( i  1 ) % M] );
last = i;
}
}
}
}
int main()
{
const size_t N = 3;
const size_t M = 4;
int a[N][M];
std::srand( ( unsigned int )std::time( nullptr ) );
for ( size_t i = 0; i < N; i++ )
{
for ( size_t j = 0; j < M; j++ ) a[i][j] = std::rand() % ( M * N );
}
for ( size_t i = 0; i < N; i++ )
{
for ( size_t j = 0; j < M; j++ )
{
std::cout << std::setw( 2 ) << a[i][j] << ' ';
}
std::cout << std::endl;
}
std::cout << std::endl;
bubble_sort( a );
for ( size_t i = 0; i < N; i++ )
{
for ( size_t j = 0; j < M; j++ )
{
std::cout << std::setw( 2 ) << a[i][j] << ' ';
}
std::cout << std::endl;
}
return 0;
}

Why am not getting any output for this program to bubble sort an array using recursion?
By : bbal
Date : March 29 2020, 07:55 AM
around this issue Depending on the number of elements: 1 : (count == n  1) is false so you do not print 2 : (count == n  1) is always true so you print, and you sorted the array >1 : (count == n  1) is always false so you nevert print (to be sure I used brutal force to check) code :
#include <stdio.h>
#include <stdlib.h>
int sort(int a[], int max)
{
if (max == 0)
return 0;
int modified;
if (a[0] > a[1]) {
int v = a[0];
a[0] = a[1];
a[1] = v;
modified = 1;
}
else
modified = 0;
return (sort(a + 1, max  1) && sort(a, max  1))  modified;
}
int main ()
{
int n, i;
printf ("enter the number of elements\n");
if ((scanf ("%d", &n) != 1)  (n <= 0))
return 1;
int * a = malloc(n * sizeof(int));
if (a == NULL) {
puts("not enough memory");
return 1;
}
printf ("enter the array elements\n");
for (i = 0; i < n; i++) {
if (scanf ("%d", &a[i]) != 1) {
puts("invalid value");
return 1;
}
}
sort(a, n  1);
for (i = 0; i < n; i++)
printf ("%d ", a[i]);
putchar('\n');
free(a);
return 0;
}
pi@raspberrypi:/tmp $ gcc pedantic Wextra b.c
pi@raspberrypi:/tmp $ ./a.out
enter the number of elements
1
enter the array elements
1
1
pi@raspberrypi:/tmp $ ./a.out
enter the number of elements
2
enter the array elements
1 2
1 2
pi@raspberrypi:/tmp $ ./a.out
enter the number of elements
2
enter the array elements
2 1
1 2
pi@raspberrypi:/tmp $ ./a.out
enter the number of elements
4
enter the array elements
4 3 2 1
1 2 3 4

