Is following C code is written according to selection sort algorithm? I'm little bit confused about it. However, it gives correct results.
#include <stdio.h>
int main() {
int n;
printf("Enter size of array: ");
scanf("%d", &n);
int a[n], i = 0, j, min;
while (i < n) {
printf("Enter no. %d:", i + 1);
scanf("%d", &a[i]);
i++;
}
for (i = 0; i < n; i++) {
for (j = i, min = a[i]; j < n; j++) {
if (a[j] < min)
min = a[j];
else
continue;
a[j] = a[i];
a[i] = min;
}
}
for (i = 0; i < n; i++)
printf("%d ", a[i]);
return 0;
}
Selection Sort
#include<stdio.h>
int main() {
int array[100], n, c, d, position, swap;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
for (c = 0; c < (n - 1); c++)
{
position = c;
for (d = c + 1; d < n; d++)
{
if (array[position] > array[d])
position = d;
}
if (position != c)
{
swap = array[c];
array[c] = array[position];
array[position] = swap;
}
}
printf("Sorted list in ascending order:\n");
for (c = 0; c < n; c++)
printf("%d\n", array[c]);
return 0;
}
Related
#include<stdio.h>
int a, b;
float arr[10][10];
void iden(float x[a][b]);
void diag(float x[a][b]);
void pri_sec(float x[a][b]);
int main() {
int i, j, n;
arr[a][b];
for (i = 0;; i++) {
printf("Enter the no. of rows and columns:\n");
scanf("%d %d", &a, &b);
if (a == b) {
printf("Enter the values in an array:\n");
for (i = 0; i < a; i++) {
for (j = 0; j < b; j++)
scanf("%f", &(arr[i][j]));
}
printf("Enter 1 to check it is an identity matrix or not, 2 to check it is diagonal matrix or not\
,3 to check principal and secondary diagonal are equal or not and any other no. to exit:\n");
scanf("%d", &n);
if (n == 1)
iden(arr);
if (n == 2)
diag(arr);
if (n == 3)
pri_sec(arr);
else
return 0;
} else {
printf("It is not a square matrix:\n");
return 0;
}
}
}
void iden(float x[a][b]) {
int i, j, flag = 1;
for (i = 0; i < a; i++) {
for (j = 0; j < b; j++) {
if (x[i][j] != 1 && x[j][i] != 0)
flag = 0;
}
}
if (flag == 1)
printf("It is an identity matrix:\n");
else
printf("It is not an identity matrix:\n");
}
void diag(float x[a][b]) {
int i, j, flag = 0;
for (i = 0; i < a; i++) {
for (j = 0; j < a; j++) {
if ((i == j && x[i][j] == 0) || (i != j && x[i][j] != 0)) {
flag = 1;
break;
}
}
}
if (flag == 0)
printf("It is a diagonal matrix:\n");
else
printf("It is not a diagonal matrix:\n");
}
void pri_sec(float x[a][b]) {
int i, k, flag = 0;
for (i = 0; i < a; i++) {
k = a - 1 - i;
if (x[i][i] != x[i][k]) {
flag = 1;
}
}
if (flag == 1)
printf("The principal and secondary diagonals are not equal:\n");
else
printf("The principal and secondary diagonals are equal:\n");
}
When I changed the indices as 10 10 for the functions it worked but for the other case it didn't.
#include <stdio.h>
int x[10][10];
int ROWS, COLS;
void func_vla(int array[10][10]) {
int i, j;
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++) {
array[i][j] = i * j;
}
}
}
int main() {
scanf("%d %d", &ROWS, &COLS);
int x[ROWS][COLS], i, j;
func_vla(x);
for (i = 0; i < ROWS; i++)
for (j = 0; j < COLS; j++)
printf("%d\n", x[i][j]);
}
#include <stdio.h>
int main() {
int n, i, j, k, l;
int temp;
printf("Enter how many element on the array : ");
scanf("%d", &n);
int arr1[100] = {};
for (i = 0; i < n; i++) {
scanf("%d", &arr1[i]);
}
for (j = 0; j < n; j++) {
for (k = j + 1; k < n; k++) {
if (arr1[j] > arr1[k]) {
temp = arr1[j];
arr1[j] = arr1[k];
arr1[k] = temp;
}
}
}
for (i = 0; i < n; i++) {
printf("%d \t", arr1[i]);
}
}
My code for sorting an array in ascending order works properly. And it doesn't have any error but when I am changed the array size then the code doesn't work properly and has an error called stack smashing detected. What causes this problem?
#include <stdio.h>
int main() {
int n, i, j, k, l;
int temp;
printf("Enter how many element on the array : ");
scanf("%d", &n);
int arr1[] = {};
for (i = 0; i < n; i++) {
scanf("%d", &arr1[i]);
}
for (j = 0; j < n; j++) {
for (k = j + 1; k < n; k++) {
if (arr1[j] > arr1[k]) {
temp = arr1[j];
arr1[j] = arr1[k];
arr1[k] = temp;
}
}
}
for (i = 0; i < n; i++) {
printf("%d \t", arr1[i]);
}
}
Neither int arr1[100] = {}; nor int arr1[] = {}; is valid C code.
The program compiles because your compiler implements GNU extensions that allow empty initializers and zero length arrays.
The reason your program no longer works when you remove the length 100 is the array becomes too short for the elements you try and store into it.
You probably meant to write int arr1[n] = {}; which does not compile because VLAs (variable sized arrays) cannot have an initializer.
Here is a modified version:
#include <stdio.h>
int main() {
int n, i, j, k, l;
printf("Enter how many element on the array : ");
if (scanf("%d", &n) != 1 || n <= 0) {
fprintf(stderr, "invalid size\n");
return 1;
}
int arr1[n];
for (i = 0; i < n; i++) {
if (scanf("%d", &arr1[i]) != 1) {
fprintf(stderr, "invalid input\n");
return 1;
}
}
for (j = 0; j < n; j++) {
for (k = j + 1; k < n; k++) {
if (arr1[j] > arr1[k]) {
int temp = arr1[j];
arr1[j] = arr1[k];
arr1[k] = temp;
}
}
}
for (i = 0; i < n; i++) {
printf("%d%c", arr1[i], "\t\n"[i == n - 1]);
}
return 0;
}
This is the code i am talking about. It checks if a matrix is sparse or not . If it is then it changes it to sparse form and transposes it. When I run it with Vscode it runs and displays till it is sparse matrix but doesn't show its sparse form and transpose form but on online compiler it runs successfully till the end.
#include <stdio.h>
int checksparse();
int changematrix();
int main()
{
int a[100][100];
int i, j, r, c;
printf("Enter number of rows and columns in the matrix\n");
scanf("%d%d", &r, &c);
printf("\nEnter the elements in the matrix");
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
printf("\na[%d][%d] = ", i, j);
scanf("%d", &a[i][j]);
}
}
int check = checksparse(a, r, c);
if (check == 1)
{
printf("\nIts a sparse matrix\n");
changematrix(a, r, c);
}
else if (check == 0)
{
printf("\nIts not a sparse matrix");
}
return 0;
}
int checksparse(int array[100][100], int r, int c)
{
int counter = 0, flag = 1;
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
if (array[i][j] == 0)
counter++;
}
}
if (counter > ((r * c) / 2))
{
return flag;
}
else
{
flag = 0;
return flag;
}
}
int changematrix(int array[100][100], int r, int c)
{
int b[100][100], temp, counter = 0;
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
if (array[i][j] != 0)
{
b[temp][0] = i;
b[temp][1] = j;
b[temp][2] = array[i][j];
temp++;
counter++;
}
}
}
printf("\nSparse Matrix : \n");
for (int i = 0; i < counter; i++)
{
for (int j = 0; j < 3; j++)
{
printf("%d ", b[i][j]);
printf("\t");
}
printf("\n");
}
printf("\nTranspose of Sparse Matrix : \n");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < counter; j++)
{
printf("%d ", b[j][i]);
printf("\t");
}
printf("\n");
}
return 0;
}
I am trying to implement Bubble sort in C and have come this far but its nor sorting properly.
#include<stdio.h>
int main()
{
int n, i, j, a[5], b, temp;
printf("Enter the number of elements to be sorted\n");
scanf("%d", &n);
for(i = 0; i < n; ++i)
{
printf("%d - Enter the elements - ", i);
scanf("%d", &a[i]);
}
for(i = 0; i < n; ++i)
{
for(j = 0; j < n+1; ++j)
{
if(a[i] > a[i+1])
{
temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
}
}
}
for (i = 0; i < n; ++i)
{
printf("%d\n", a[i]);
}
return 0;
}
Input
2
12
1
13
Output
2
1
12
13
What am I missing ?
So now that the drama is behind us, the problem with your code was that you were not using the proper index in your inner loop. In addition your inner loop counter's conditional check was not correct. Also, as I mentioned in a comment to your question, you have a flaw in your code (which I have not fixed) where you initialize your array prior to asking the user how many elements they want to enter. This can lead to an index out of bounds exception if the user enters a number greater than 5.
#include<stdio.h>
int main()
{
int n, i, j, a[5], b, temp;
printf("Enter the number of elements to be sorted\n");
scanf("%d", &n);
for(i = 0; i < n; ++i)
{
printf("%d - Enter the elements - ", i);
scanf("%d", &a[i]);
}
for(i = 0; i < n; i++)
{
for(j = 0; j < n-1; j++)
{
if(a[j] > a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
for (i = 0; i < n; ++i)
{
printf("%d\n", a[i]);
}
return 0;
}
your second loop is not proper.
for(j=0;j<n-i-1;j++){
}
/*By your approach , in inner loop you are not checking the each elements . So change i to j in swapping , and limit of j should be till n-1*/
#include<stdio.h>
int main()
{
int n, i, j, a[10], b, temp=0;
printf("Enter the number of elements to be sorted\n");
scanf("%d", &n);
for(i = 0; i < n; ++i)
{
printf("%d - Enter the elements - ", i);
scanf("%d", &a[i]);
}
for(i = 0; i < n; ++i)
{
for(j = 0; j < n-1; ++j) // notice limit , also complexity can be reduced by changing to(j<n-i-1)
{
if(a[j] > a[j+1])
{
temp = a[j]; // changed variable
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
for (i = 0; i < n; ++i)
{
printf("%d\n", a[i]);
}
return 0;
}
/*You May check this code it will help you*/
#include<stdio.h>
void bubble_sort(int a[],int n);
void bubble_sort(int a[],int n)
{
int i,j;
int temp;
for(i=0;i<n;i++)
{
for (j=0; j<=n-i-1;j++)
{
if (a[j]>a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
}
main()
{
int a[10];
int i,n;
printf("Enter the number\n");
scanf("%d",&n);
printf("Enter the number\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
bubble_sort(a,n);
printf("sorted elements are\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
}
#include<stdio.h>
int main()
{
int n, i, j, a[5], b, temp;
printf("Enter the number of elements to be sorted\n");
scanf("%d", &n);
for(i = 0; i < n; ++i)
{
printf("%d - Enter the elements - ", i);
scanf("%d", &a[i]);
}
for(i = 0; i < n; ++i)
{
for(j = 0; j < n; ++j)
{
if(a[j] > a[j+1]) //change the varible instead of i to j
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
for (i = 0; i < n; ++i)
{
printf("%d\n", a[i]);
}
return 0;
}
I've tried to cover all possible conditions to reduce passes and comparisons for bubble sort to reduce the overall time taken. Here goes my code...
#include <stdio.h>
#include <conio.h>
void bubbleSort(int n){
int arr[n],i,j,temp=0;
int swapFlag = 0;
printf("\nInsert %d elements:\n",n);
for(i=0;i<n;i++){
scanf("%d",&arr[i]);
}
printf("Insert complete.\n\n");
printf("Your array looks like:\n");
for(i=0;i<n;i++){
printf("%d ",arr[i]);
}
//Bubble Sort Algorithm
for(i=0;i<n-1;i++){
swapFlag = 0;
for(j=0;j<n-i-1;j++){
if(arr[j]>arr[j+1]){
swapFlag = 1;
//Swapping unordered pairs
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
//Condition to reduce number of passes & comparisons
if(swapFlag == 0){
break;
}
}
printf("\n\nAfter sorting the array looks like:\n");
for(i=0;i<n;i++){
printf("%d ",arr[i]);
}
}
void main(){
int n;
printf("Enter number of array elements: ");
scanf("%d",&n);
bubbleSort(n);
getch();
}
Result:-
I am writing a program that arranges an array in nondecreasing order; then, it inserts a value into the sequence. I can easily get numbers in the beginning and the middle of the array, but whenever I add a number that should go at the end, I keep getting 0. Where am I going wrong?
#include <stdio.h>
int main()
{
int array[10];
int i, j, n, m, temp, key, pos;
printf("Enter number of elements:\n");
scanf("%d", &n);
printf("Enter the elements:\n");
for (i = 0; i < n; i++)
{
scanf("%d", &array[i]);
}
printf("Input array elements:\n");
for (i = 0; i < n; i++)
{
printf("%d\n", array[i]);
}
for (i = 0; i < n; i++)
{
for (j = i + 1; j < n; j++)
{
if (array[i] > array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
printf("Sorted list is\n");
for (i = 0; i < n; i++)
{
printf("%d\n", array[i]);
}
printf("Enter the element to be inserted X:\n");
scanf("%d", &key);
for (i = 0; ; i++)
{
if (key < array[i])
{
pos = i;
break;
}
}
m = n - pos + 1 ;
for (i = 0; i <= m; i++)
{
array[n - i + 2] = array[n - i + 1] ;
}
array[pos] = key;
printf("Final list is:\n");
for (i = 0; i < n + 1; i++)
{
printf("%d\n", array[i]);
}
}
If the number to be entered is larger than all elements the following loop poses an issue.
...
for (i = 0; ; i++)
{
if (key < array[i])
{
pos = i;
break;
}
}
If the number is largest,then the pos will be junk and not n.The default value of an array is junk.
Replace it with this.
for (i = 0;i<n ; i++)
{
if (key < array[i])
{
break;
}
}
pos = i;
...