logo
down
shadow

Java program crashes with StackOverflow when using bubble sort of a large array


Java program crashes with StackOverflow when using bubble sort of a large array

By : ColoBrian
Date : November 21 2020, 04:03 PM
this one helps. Keeping aside the logic, the technical reason as to why it fails at 10000 is because each thread in Java has a fixed stack size. And when you use 10000 it is unable to find enough memory.
Use -XX:ThreadStackSize=512 to increase the default memory that JVM allocates to threads and it may work. But generally you don't have to bother about it.
code :


Share : facebook icon twitter icon
Bubble sort function for strings causes program crashes

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 hard-code 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?

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

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

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 two-dimensional array to pointer to int and sort the array as a one-dimensional 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[i-1] )
            {
                std::swap( a[i], a[i-1] );
                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?

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 
Related Posts Related Posts :
  • How To Check The If A Nested Loops Is Done?
  • Is it fine to use jpa repositories and hibernate(calling queries using criteria,HQL) in the same project
  • Removing substrings from java StringBuilder
  • How to insert some data into an array of index of a class with for loop and how to view them?
  • Can ActiveMQ cluster have brokers running on different transport protocol?
  • How do I get my caesar cipher to read spaces in my menu option
  • I want to contains all elements of array to another array
  • How to list only the folders within a specified directory?
  • I'm having problems making an app that finds user lat/lng and device IMEI
  • RestTemplate get body with ResourceAccessException
  • SocketException: Connection reset on JRE 1.6, not on JRE 11
  • Regex to retrieve any alphanumeric otp code
  • How to get data json from array of the year in array android studio?
  • RoaringBitmap is serialized to different string using node and java
  • File.lastmodified() generates wrong date and month
  • Spring Boot Sql Date Timestamp Json Serialization Issue
  • Why this BiConsumer Combiner code in collect method is never reached?
  • How to assign Nth value of an array to variable
  • Java: How to make RecyclerView item number change to the same number in Activity? (real time)
  • How to send Data when cardview clicked to detail content
  • Why the order is reverse in equals()
  • Adding object to Arraylist in one frame but then cannot access it in the second already opened frame
  • Cannot resolve symbol Theme.MaterialComponents.Light.NoActionBar (Android Studio)
  • I don't understand the solution of this exercise
  • Why does my array list alwas has a size of one?
  • How to resolve "Access Not Configured" for Youtube Data API
  • Linear Layout grid showing in emulator but not on device, what could be cause?
  • How to draw on top of scaled Image in java?
  • Spring Data Mongo - Inheritance and Embeddable
  • How to turn an integer value into a double decimal value?
  • Access dockerfile ENV variables in entrypoint
  • Spring MVC model binding not working for non public members
  • How can I wrap the int[] into an object and implement equals()?
  • Weird problem with my favicon, not showing up in some views
  • RecyclerView inside of NavigationDrawer not scrolling
  • GAE Getting lots of CancellationException for Task Queue DataStore call
  • Add method annotation at runtime with Byte Buddy
  • Getting a nullpointer exception in variable initializer
  • How to get the question to loop if the user input is incorrect
  • Is there a generic way to work with optimistic locking using Hibernate/Spring Data JPA?
  • Equivalent Inflate for Deflate function in Java
  • Can you use an int array as an objects parameter?
  • Creating Predicates on the fly
  • Returning value from persisted Groovy code without using java.io.File
  • Difference between a hashmap and an array in time complexity of a lookup
  • Placeholder for TreeView?
  • What is the section between the package name and the class activity called
  • InvalidSelectorException: invalid selector: Compound class names not permitted
  • JdbcTemplate.queryforList() returns value but map.get returns null
  • Convert CountDownTimer Cancel feature to RxJava
  • @RunWith(CucumberWithSerenity.class) throws NoClassDefFound cucumber/runtime/junit/Assertions
  • Jackson deserialize to list of child elements
  • Array of eight random Integers where each Integer has a difference of at least 15 from all others in the array?
  • Not getting the file extension right when serving a file
  • How to use @Autowired on field variable when class is not a Bean?
  • How to calculate a percentage as an exponential progression?
  • How to add text to a JComboBox when using IntelliJ UI Designer?
  • TableColumn #setCellValueFactory For Class With HashMaps
  • How do I access the ApplicationContext from a service within the micronaut framework?
  • Accessing a micro-service from Java application
  • shadow
    Privacy Policy - Terms - Contact Us © festivalmusicasacra.org