## Bubble sort takes a long time then segfaults

Given an integer

`n`

, write a C program to count the number of digits that are in the same position after forming an integer`m`

with the digits in`n`

but in ascending order of digits. For example, if the value of`n`

is 351462987 then value of`m`

will be 123456789 and digits 4 and 8 will be in the same position.

This is my code:

#include<stdio.h> void bubble(int a[],int length) { for (int i=0;i<length;i++) { for (int j=0;j<length;j++) { if (a[j]>a[j+1]) { int t=a[j]; a[j]=a[j+1]; a[j+1]=t; } } } } int check(int a[],int b[],int length) { int count=0; for (int i=0;i<length;i++) { if (a[i]==b[i]) { count=i; break; } } return count; } int length(int n) { int l; while (n!=0) { n=n/10; l++; } return l; } void main() { int n,arrn[100],temp[100]; scanf("%d",&n); int l=length(n); for (int i=0;i<l;i++) { arrn[l-i-1]=n%10; temp[l-i-1]=arrn[l-i-1]; n=n/10; } bubble(temp,l); int c=check(arrn,temp,l); printf("%d",c); }

I am able to compile the code but when I execute it it takes a long time only to show segmentation fault.

**Why am I getting a segmentation fault with my bubble sort? : cs50,** I think the bubble sort method goes in the right direction, but your FOR loops, are poorly While in college I picked up a job working at McDonalds part time, to pick up some extra money. I know this was a long post so thank you if anybody actually read it all, I just really Took a break from a PSET and this came out of it. If an array is already in sorted order, and bubble sort makes no swaps, the algorithm can terminate after one pass. With this modification, if bubble sort encounters a list that is already sorted, it will finish in O (n) O(n) O (n) time. Though bubble sort is simple and easy to implement, it is highly impractical for solving most problems due to its slow running time.

Your length function has a very obvious bug in it. What value does `l`

start with? You don't initialise it so it could start with any value and cause undefined behaviour. You should set it to 0.

int length(int n) { int l = 0; while (n!=0) { n=n/10; l++; } return l; }

**c++,** It also stopped the string sorting from causing a Segmentation Fault. your sort more efficient, one of the features of bubble sort is that after each iteration the top Bubble sort is a sorting algorithm, It works by comparing each pair of adjacent elements and switching their positions if necessary. It repeats this process until all the elements are sorted. The average and worst-case time complexity of bubble sort is – O (n2)

Personally, I wouldn't be sorting or reading it into an int - to enable handling leading zeros in the digit string. For example:

#include <stdio.h> #include <string.h> #include <ctype.h> #define MAXNUMLEN 200 int main(void) { int i, j, l, x=0; char numin[MAXNUMLEN], numout[MAXNUMLEN]; int digits[10]={0}; printf("enter a string of digits: " ); fgets(numin, sizeof(numin), stdin); printf("\nsaw : %s", numin ); // walk string once, counting num of each digit present l=strlen(numin); for(i=0; i<l; i++) { if( isdigit(numin[i]) ) { int d = numin[i] - '0'; // char digit to int digit digits[d]++; } } // for each digit present, write the number of instances of the digit to numout for( i=0; i<10; i++ ) { for(j=0; j<digits[i]; j++) numout[x++] = '0'+i; // int digit back to char digit } numout[x]='\0'; // terminate string printf("sorted: %s\n", numout ); }

Sample run:

watson:digsort john$ ./ds enter a string of digits: 002342123492738234610 saw : 002342123492738234610 sorted: 000112222233334446789 watson:digsort john$

**Reviewing C++,** When dealing with any kind of array, you can certainly sort them. Three of which will be discussed here: selection sort, bubble sort and count sort. //found the smallest elements index so swap here: if(arr[i] != arr[indexMin]){ int temp = arr[i]; When the time comes for swapping values, the index is needed, not the value. Large time complexity of Bubble sort algorithm is its main disadvantage. Because of worst case complexity of bubble sort algorithm is O(N^2), a lot more time it takes to sort a list with having huge number of elements. Thus bubble sort is more suitable for teaching sorting algorithms instead of real python applications.

**Time Comparison of Quick Sort, Insertion Sort and Bubble Sort ,** And I had to take some medium performing algorithm, so Insertion Sort was chosen. All three of them are quite popular and easy to understand. The Bubble Sort¶ The bubble sort makes multiple passes through a list. It compares adjacent items and exchanges those that are out of order. Each pass through the list places the next largest value in its proper place. In essence, each item “bubbles” up to the location where it belongs. Figure 1 shows the first pass of a bubble sort. The

**help with a segmentation fault - C Board,** bubble sort to sort it, prints it again, and then writes it to the the header for the main() function so that // it takes in command-line arguments int array 'array'. preconditions: - 'array' is an integer array large enough to hold all of file %s could not be opened\n", fname); exit(1); } while(i<size) { // use fprintf to How bubble sort compares two elements. Well, we know that bubble sort will compare two pairs at a time. Naturally, it will start off comparing the first two elements in our list — the first pair.

**CS&SE,** Your computer is so fast that you need to do a lot of computations for the time Write the bubble sort algorithm in C and check timing with larger and larger input sizes. and see how large an array you can allocate without segmentation fault. our programs, we have to know how much time our program takes to execute. Bubble sort is also known as sinking sort (heavy or bigger elements settles down at the bottom of the list after each iteration). Time Complexity: O(N 2) Let’s walk through on example: Pros and cons of Bubble sort: Pros: Bubble sort algorithm is considered as very simple sorting technique since all you need to do is compare all the adjacent