This is an example of a c program to sort a list of names... I'm new to algorithms that's why I need to know what type it is!
What are real life examples could I use it for too?
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main() {
char *str[5], *temp;
int i, j, n;
printf("\nHow many names do you want to have?");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("\nEnter the name %d: ", i);
flushall();
gets(str[i]);
}
for (i = 0; i < n; i++) {
for (j = 0; j < n - 1; j++) {
if (strcmp(str[j], str[j + 1]) > 0) {
strcpy(temp, str[j]);
strcpy(str[j], str[j + 1]);
strcpy(str[j + 1], temp);
}
}
}
flushall();
printf("\nSorted List : ");
for (i = 0; i < n; i++)
puts(str[i]);
return (0);
}
I hope its a bubble sorting. And with this you can arrange numbers in ascending or descending order.
First of all the program is invalid. It does not allocate memory where it is going to store strings. As result it is a very risky step to ask the user
how many names does he want to have because there is no space to store strings in the program.:)
You could use a two-dimensional variable length character array for the strings and standard function fgets instead of gets to enter the strings.
As for the sort algorithm then it is a bad realization of the bubble sort.:)
Related
The task was simply to write a program to sort the entered (or randomly generated) numbers using the Shell sort method. But then the teacher decided to complicate the task and said:
Create another function that will test Shell's algorithm: create many arrays from n random numbers, sort by Shell's method and display the average number of iterations for all these arrays.
Please, help and explain how to do it
This is my code:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void shellsort(int v[], int n);
int main() {
srand(time(NULL));
int N;
printf("Input N: ");
scanf_s("%d", &N);
int *a = (int *)malloc(N * sizeof(int));
int i = 0;
printf("Enter masiv: ");
for (; i < N; i++) {
a[i] = rand() % 100;
printf("%d ", a[i]);
}
shellsort(a, N);
printf("\n");
printf("Result: ");
for (i = 0; i < N; i++) {
printf("%d ", a[i]);
}
printf("\n");
return 0;
}
void shellsort(int v[], int n) {
int i, j, gap, temp;
int num = 0;
for (gap = n / 2; gap > 0; gap /= 2) {
for (i = gap; i < n; i++)
for (j = i - gap; j >= 0 && v[j] > v[j + gap]; j -= gap) {
temp = v[j];
v[j] = v[j + gap];
v[j + gap] = temp;
num++;
}
}
printf("\n");
printf("Number of iterations: %d\n", num);
}
Many thanks in advance to everyone who responds;))
Alright, I will start by saying that I am not gonna do your homework and write the code for it. I'll just try to give you a starting point.
Shell sort currently doesn't compute the number of iterations, so you might declare a variable named iterations at the beginning of shellsort and increment it in the deepest loop inside shell sort, and return this variable instead of returning void.
The new function that you're about to write should be similar to what you're doing in the main for the number generation part.
Make a loop that encapsulates the loop that generates arrays for you. Because you need multiple arrays now.
Store the sum of iterations, maybe like this sum += shellsort(a);
Then the average of iterations is the sum/N, where N is the number of random arrays you want to generate.
I want to add the element in an array while entering it in a sorted manner. That means at the time of entering the new element in array, it will add its sorted state.
I have tried following examples, but I want to do it in an automatic manner. Can anyone help me, please?
my example
#include <stdio.h>
void main()
{
int a[20], n, item, i;
printf("Enter the size of the array");
scanf("%d", &n);
printf("Enter elements of the array in the sorted order");
for (i = 0; i<n; i++)
{
scanf("%d", &a[i]);
}
printf("\nEnter ITEM to be inserted : ");
scanf("%d", &item);
i = n - 1;
while (item<a[i] && i >= 0)
{
a[i + 1] = a[i];
i--;
}
a[i + 1] = item;
n++;
printf("\n\nAfter insertion array is :\n");
for (i = 0; i<n; i++)
{
printf("\n%d", a[i]);
}
getch();
}
I have a task to sort negative and positive numbers while using dynamic memory, therefore in this case I used calloc and bubble sort to arrange negative numbers first while not changing their order. The problem is when I enter an even number of integers, in the middle of the result some random negative number of 10 digits appears. The same doesn't happen with odd number of integers. What seems to be the problem?
#include <stdio.h>
#include <stdlib.h>
#define SIZE 1000
void swap(int *arr, int n) {
int i, j, temp;
for (i = 0; i <= n; i++) {
for (j = 1; j <= n; j++) {
if (arr[j] < 0) {
temp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = temp;
}
}
}
printf("sorted integers to negative and positive: \n");
for (i = 0; i < n; i++) {
printf("%i ", arr[i]);
}
}
int main() {
int n;
int i, *sk;
printf("Enter integer: \n");
scanf("%d", &n);
printf("Enter %i integers: \n", n);
sk = (int*)calloc(sizeof(int), n);
for (i = 0; i < n; i++) {
scanf("%d", sk + i);
}
swap(sk, n);
return 0;
}
This is undefined behavior that happens to manifest itself to you only when you happen to enter an even number of integers, but in reality the problem is always there: you read a value from one-past-the-end of the array, and it makes its way to the middle of your array.
You can fix this behavior by changing i <= n and j <= n with i < n and j < n. However, this is not going to fix your broken sorting algorithm, because the swapping condition is incorrect as well. Instead of
if(arr[j]<0)
it should be
if(arr[j]<arr[j-1])
You have 2 classic bugs in your for loops:
for (i = 0; i <= n; i++) is almost always wrong because the loop is run n + 1 times, where it should only enumerate index values from 0 to n - 1.
You have the same off by one error in the second loop: the test j <= n makes you go one step too far and read beyond the end of the array. Some random value gets shuffled into the array, but this undefined behavior could have worse consequences.
Furthermore, your comparison test is incorrect, it should be if (arr[j] < arr[j-1]).
As a rule of thumb, whenever you see the <= operator in a loop test, look again, it is probably a bug.
Here is a corrected version:
void swap(int *arr, int n) {
int i, j, temp;
for (i = 0; i < n; i++) {
for (j = 1; j < n; j++) {
if (arr[j] < arr[j - 1]) {
temp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = temp;
}
}
}
printf("sorted integers to negative and positive: \n");
for (i = 0; i < n; i++) {
printf("%i ", arr[i]);
}
printf("\n");
}
This is the simplest insertion sort program.
Unfortunately, it doesn't provide an outcome:
it does prompt the user for the size of the array
and for the list of numbers, but it doesn't do
the sort. I would be grateful for your help!
/** Insertion sort **/
#include <stdio.h>
int main (void)
{
int size, array[80], i, j, element;
printf("Enter number of elements: \n");
scanf ("%d", &size);
printf("Enter %d integers\n", size);
for (i = 0; i < size; i++)
{
scanf("%d", &array[i]);
}
for (i = 0; i < size; i++)
{
element = array[i];
j = i;
while (j > 0 && array[j-1] > element)
{
array[j] = array[j-1];
array[j-1] = element;
j--;
}
}
printf ("Sorted list in ascending order:\n");
for (i = 0; i < size; i++)
printf ("%d", array[i]);
return 0;
}
This is one of those confusing moments :)
I have forgotten that there is a '\n' character
in the second printf, after which the user is
prompted for the list. Therefore, I've kept typing
in numbers in one row, without hitting ENTER; of course,
the program considered that as one number, only one integer
not the list ))) Thank you everyone for replying.
Sorry for this.
I am trying to learn C and I am trying to write a piece of code which does the following:
Take user input of a natural number n
Take user input of n elements and store them in the array x
Delete all negative numbers from the array x
Print the new array, with the length n - number of deleted elements
Here is my code:
#include <stdio.h>
int main(void)
{
int n, i, count=0;
double x[1000];
scanf("%d", &n);
for (i=0; i<n; i++)
scanf("%lg", &x[i]);
for (i=0; i<n; i++)
{
if (x[i] < 0)
{
count++;
continue;
};
x[i-count]=x[i];
};
n -= count;
for (i=0; i<n; i++)
printf("%d: %g\n", i, x[i]);
return 0;
}
I have been told that I should replace my second for loop with the following code:
int j=0
...
for (i=0; i<n; i++)
{
if (x[i] < 0)
{
count++;
continue;
};
if (i > j)
x[j] = x[i];
j++;
};
Could someone please explain why is the latter code better?
If i==j, then you're assigning an element to itself: not wrong, but a (small) waste of effort.
If you really want to improve this, avoid putting the negative values in the array in the first place.