Im trying to study some algorithm questions and I can't find a way to start my select sort algorithm code. Im using Visual Studio and the code doesn't show any errors through compiling. When I start the code the system just stops for a few seconds and print "press any key to continue...". Im a beginner and I can't see what's wrong. HELP MEEE
#include <stdio.h>
int n = 6;
int qwerty(int a[]) {
int i, j, t;
for (i=1; i<=n-1; i++){
for (j=i+1; j<=n; j++){
if (a[i] > a[j]){
t = a[j];
a[j] = a[i];
a[i] = t;
}
}
}
}
int main(void) {
int a[6] = { 2, 14, 20, 8, 17, 13 };
qwerty(a[n]);
int i;
for (i = 1; i <= n; i++){
printf("%d ", a[i]);
}
}
In the C language, arrays start indexing at 0, and the last element is at the index n-1. So the first thing you should change is the loops to be for(i = 0; i < n-1; i++) and for(j = i + 1; j < n; j++)
Also, this is a bubble sort algorithm, not selection sort. Selection sort algorithms can be found anywhere so I'm not going to type it here.
Related
As a practice task I have tried implementing the bubble sort algorithm using the C programming language. The following is my code:
int main() {
int arr[5];
int i, j;
int n = sizeof(arr[0]) / sizeof(arr);
srand(time(NULL));
for (i = 0; i < n; i++) {
arr[i] = rand() % 100;
}
printf("Array before ordering:\n");
for (i = 0; i < n; i++) {
printf("%d\t", arr[i]);
}
printf("\n");
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - 1; j++) {
if (arr[j] > arr[j + 1]) {
swap(&arr[j], &arr[j + 1]);
}
}
}
printf("Array after ordering:\n");
for (i = 0; i < n; i++) {
printf("%d\t", arr[i]);
}
printf("\n");
system("PAUSE");
exit(0);
}
As you can see I have two loops to print out the array, one to print out the initial array and one to print it out after ordering it. Yet this is the output:
Array before ordering:
Array after ordering:
Press any key to continue . . .
For some reason i cannot seem to output the elements of the array. I have tried running it on Visual Studio and CLion. Can anyone see what is wrong here?
int n = sizeof(arr[0]) / sizeof(arr);
This should be the other way around. i.e. sizeof(arr) / sizeof(arr[0])
If you want to debug this, try printing n.
So here is the code....
#include <stdio.h>
#include <stdlib.h>
int main() {
int a[20] = { 1, 2, 4, 5, 6, 7, 8, 9, 10 };
int new_num, n, i = 0;
for (i; i < 10; i++) {
//printf("the elements in the array are:");
printf("%d\n",a[i]);
}
printf("enter the index you want to insert the element");
scanf("%d", &n); //don't forget to add &
printf("Enter the element");
scanf("%d", &new_num);
for (i = 9; i >= n; i--) {
for (int j = 10; j > n; j--) {
a[j] = a[i];
}
}
a[n] = new_num;
printf("new array is:");
for (i = 0; i < 10; i++) {
printf("%d\n", a[i]);
}
return 0;
}
and here is the output...
Can anyone tell me what I did wrong?
I want to use a nested loop for shifting the elements to the next address, instead of using this syntax:
for (i = 9; i >= n; i--) {
a[i + 1] = a[i];
}
but the output seems a bit eerie as I have shown in the output image.
The problem is this snippet:
for (i=9;i>=n;i--){
for (int j=10;j>n;j--){
a[j]=a[i];
}
}
When i == 4, you're setting all elements of a for which the index exceeds 4 to the value of a[4] (which is 6). You probably want something closer to:
for (int j = 10; j > n; j++) {
a[j] = a[j - 1];
}
a[n] = new_num;
Also, you have a small bug in that you're assuming a is of length 10 but you're missing the value 3, so it only has 9 elements.
Edit: I just saw that you want to use a nested loop. Is that a hard requirement? A nested loop just isn't the right way to go here.
Looking for help describing this Algorithm for sorting an array. Is it a bubble or selection sort? Why do the elements switch?
#include <stdio.h> //including stdio.h for printf and other functions
#include <conio.h> //including conio.h for _getch() and other functions
int main() //default function for call
{
int a[10] = { 2,4,6,8 }; //Array declaration size-10
int n = 4; //Temporary number for array size
printf("\n\nArray Data : ");
flushall(); //Printing message
for (int i = 0; i < n; i++) //Loop for displaying the data of array
{
printf(" %d ", a[i]); //Printing data
}
for (int i = 0; i < n; i++) //Loop for ascending ordering
{
for (int j = 0; j < n; j++) //Loop for comparing other values
{
if (a[j] > a[i]) //Comparing other array elements
{
int tmp = a[i]; //Using temporary variable for storing last value
a[i] = a[j]; //replacing value
a[j] = tmp; //storing last value
}
}
}
}
This is a less efficient way of bubble sort.
You can find explanations here :
https://en.wikipedia.org/wiki/Bubble_sort#Step-by-step_example
A better code will be
for (int i = 0; i < n-1; i++)
for (int j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1])
{
// swap temp and arr[i]
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
It reduces number of comparisons and looping by optimizing the upper limit of loops.
You can see a video on this here.
I wrote a program for bubble sort which is showing a run time error saying "NULL Pointer Assignment". The code is given below:
#include <stdio.h>
void main()
{
int a[6], j = 0, count = 0, i, temp;
printf("Enter the number");
for(i = 0; i< 4; i++)
{
scanf("%d", &a[i]);
}
while(count < 4)
{
for(i = 0; i < 4; i++)
{
if(a[i] < a[++j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
count++;
}
printf("The sorted array is");
for(i = 0; i < 4; i++)
{
printf("\n%d\n", a[i]);
}
getchar();
}
But When I tried the below code then it runs successfully.
#include <stdio.h>
void main()
{
int a[6], count=0, i, temp;
printf("Enter the number");
for(i = 0; i < 4; i++)
{
scanf("%d", &a[i]);
}
while(count<4)
{
for(i = 0; i < 4; i++)
{
if(a[i] < a[i + 1])
{
temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
}
}
count++;
}
printf("The sort array is");
for(i = 0; i < 4; i++)
{
printf("\n%d\n", a[i]);
}
getchar();
}
So I need the reason that why my code is showing error, and why the 2nd code is working. I am a new learner in C so please explain to me the reason briefly and simply.
The reason you are getting a NULL pointer exception is in the first program, by the completion of the first iteration of the while loop the value of j has become 4. In the second iteration of the while loop the j value is not reset and continues from 4. Trying to access a[j] when j is equal to 6 will result in a the said error.
for (i = 0; i < n - 1; i++) {
for (j = 0; j <= i; j++) {
if(a[j] > a[j + 1]) {
// swap
}
}
}
Something like this will do the job.
The reason is that you don't reset the variable ´j´ at the beginning of the outer while loop and j is increased in the inner for loop. So you are increasing j in each of the 4 outer while loop transitions for 4 times in the inner for loop. So j has already reached the value 4 after the first while loop round. In the second while loop round it will be incremented to 5, 6, 7, 8 etc. So just reset the variable j to 0 at the beginning of the while loop:
while(count<4)
{
j = 0;
for(...
By the way it's still not a good implementation because you are comparing the last value with an undefined value: if(a[3] < a[4]).
Whereas a[3] contains the last entered value the variable a[4] contains an undefined value. Therefore you should change the for loop to for(i=0;i<3;i++).
I am new to C and trying to learn shell sorting.I am trying to sort an integer array in ascending order.Here is my code-
#include <stdio.h>
main()
{
int a[] = {1, 9, 7, 4, 8, 6, 7,2,1,6 };
int n =10; //array length
for (int c = (n / 2); c > 0; c = c / 2)
{
for (int i = c; c < n; i++)
{
int t = a[i];
int j;
for (j = i;( j >= c) && (t < a[j - c]); j = j - c)
{
a[j] = a[j - 1];
}
a[j] = t;
}
}
for (int i = 0; i <= 9; i++)
{
printf("%d ", a[i]);
}
}
On compiling this code in Visual Studio Express an error message comes asking either to close or debug and the output terminal not shows any output.I can't figure out what is wrong in this code.I would appreciate if someone could explain, and perhaps point me to a solution that would do what I want.
Your code is almost right, just replace for (int i = c; c < n; i++) with for (int i = c; i < n; i++)
'c' will always be lesser than 'n' that's why your code is falling into infinite loop.
try to add this code....
#include<stdio.h>
#include<conio.h>
int main()
{
int arr[30];
int i,j,k,tmp,num;
printf("Enter total no. of elements : ");
scanf("%d", &num);
for(k=0; k<num; k++)
{
printf("\nEnter %d number : ",k+1);
scanf("%d",&arr[k]);
}
for(i=num/2; i>0; i=i/2)
{
for(j=i; j<num; j++)
{
for(k=j-i; k>=0; k=k-i)
{
if(arr[k+i]>=arr[k])
break;
else
{
tmp=arr[k];
arr[k]=arr[k+i];
arr[k+i]=tmp;
}
}
}
}
printf("\t**** Shell Sorting ****\n");
for(k=0; k<num; k++)
printf("%d\t",arr[k]);
getch();
return 0;
}