Adding element in sorted order in an array - c

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();
}

Related

printing the largest value in array

I was having a trouble regarding to printing out the largest number in array in c language and i don't know how to do it while using loop. Please help me thanks
here is my code
#include <stdio.h>
void main() {
int Lnum, size;
printf("Enter the size: ");
scanf("%d", &size);
int nums[size];
for (int i = 0; i < size; i++) {
scanf("%d", &nums[i]);
}
printf("\nReversed = ");
for (int i = size - 1; i >= 0; i--) {
printf("%d ", nums[i]);
}
for (int a = 0; a < size; a++) {
for (int i = 0; i < size; i++) {
if (nums[a] > nums[i]) {
Lnum = nums[a];
}
}
}
printf("\nLargest element = %d", Lnum);
}
The algorithm for determining the largest element in an array goes as follows.
Use the value of the first array element as a start value for the biggest number.
Go over the rest of the array and check for each element if it is a bigger number.
When a bigger number is found use the bigger number for further comparisons.
#include<stdio.h>
void main(){
int Lnum, size;
printf("Enter the size: ");
scanf("%d", &size);
int nums[size];
for(int i=0; i<size; i++){
scanf("%d", &nums[i]);
}
printf("\nReversed = ");
for(int i=size-1; i>=0; i--){
printf("%d ", nums[i]);
}
Lnum = nums[0]; // Assume first element to be the largest element
for(int a=1; a<size; a++){ // start loop with second element
if(Lnum < nums[a]) { // check if current element is larger
Lnum = nums[a]; // found larger element, it is now the largest element of all inspected element
}
}
printf("\nLargest element = %d", Lnum);
}
Output:
Enter the size: 3
1 2 3
Reversed = 3 2 1
Largest element = 3
...Program finished with exit code 0

How do I determine the most swapped number of an array in selection sort?

I made a program to do a selection sort and determine which are the most swapped elements of the array. For example, if the elements of the array = 8 12 6 9 1 3, the most swapped number is 8. It's swapped 2 times. For another case, how do I find the most swapped number and how many times did it swap?
#include<stdio.h>
int main(){
int case, numbers, array[50], i,j,k,c,d, min,temp, swap=0;
//Number of cases
printf("case = ");
scanf("%d", &case);
//inserting the elements of array
for(i=0; i<case; i++){
printf("total of numbers = ");
scanf("%d", &numbers);
for(j=0; j<numbers; j++){
scanf("%d", &array[j]);
}
//Selection Sort
for (c = 0; c < (numbers - 1); c++)
{
min = c;
for (d = c + 1; d < numbers; d++){
if (array[min] > array[d])
min = d;
}
if (min != c){
temp = array[c];
array[c] = array[min];
array[min] = temp;
}
}
//Sorting result
printf("Hasil:");
for(k=0; k<numbers; k++){
printf("%d ", array[k]);
}
printf("\n");
}
return 0;
}
Please help me if you understand. Thank you so much!

Sorting a 2D-Array into Ascending order

I am trying to print out the contents of an Array into Ascending order, i am also using Functions (part of a task i have been assigned) - i am fairly new to C/C++ hence if anything is a mess.
The Sorting algorithm i have used just outputs all the numbers into a random order, any way to resolve or correct this? - Ideally this needs to output into the 2x2 grid as the Array Displays.
Ascending Order Output
include <stdio.h>/*Seperate Function for Input ArrayInt */
int main(){
int arrayHeight, array[100][2], xCoord, yCoord, i;
printf ("***** Bubble Sort ***** \n");
printf("\n How many items of data do you wish to enter? ");
scanf("%d",&arrayHeight);
for(i=0; i<arrayHeight; i++){
printf("Please enter in the X coordinate: ");
scanf("%d", &xCoord);
printf("Please enter in the Y coordinate: ");
scanf("%d", &yCoord);
array[i][0] = xCoord;
array[i][1] = yCoord;
}
DisplayArray(array, arrayHeight);
BubbleSort(array, arrayHeight);
}
int DisplayArray(int array[100][2],int arrayHeight, int swap) {
/*Displaying Array elements*/
int i, j;
printf("\n The 2-D Array contains : \n");
for(i=0; i<arrayHeight; i++)
{
printf("[%d][%d]\n\r", array[i][1], array[i][0]);
}
}
int BubbleSort(int array[100][2], int arrayHeight)
{
/*Sorts the Array elements into appropriate chosen sorting order - Ascending*/
int swap, i, j, k;
for(k =0; k< arrayHeight; k++) {
for (i = 0; i <arrayHeight; i++) {
for (j = i+1; j < 3; ++j) {
if (array[i][0] > array[i][-1]) {
array[i][1] = swap;
swap = array[i][0]; /*Array = Array Name*/
}
}
}
}
printf("\n Printing in Asending Order: ");
{
for (i=0; i<arrayHeight; i++)
{
for (j=0; j<arrayHeight; j++)
{
printf("\n [%d][%d] ", array[i][-1], array[i][1]);
}
}
}
}

How can I get all matching values with a binary search?

Here is my code for finding the position of a value in a sorted array using a binary search. My problem is that the value may be present in my array more than one time, but my search only shows the position of the first match it finds. I want to output my all matching value's position. How can I solve my issue?
Thanks in advance.
#include<stdio.h>
#include<stdlib.h>
void aSort(int *a, int aLen){
int i, j;
for(i=0; i<aLen-1; i++){
for(j=i+1; j<aLen; j++){
if(a[i] > a[j]){
int temp;
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
return;
}
void bSearch(int *a, int aSize, int key,int *rArr, int *ItemNum){
int start = 0;
int aEnd = aSize-1;
int mid = (start + aEnd) / 2;
*ItemNum = -1;
while(start<=aEnd){
if(key == a[mid]){
*ItemNum += 1;
rArr[*ItemNum] = mid;
break;
}else if(a[mid] < key){
start = mid + 1;
}else{
aEnd = mid - 1;
}
mid = (start + aEnd) / 2;
}
return;
}
int main(){
int n;
char i_type;
printf("Enter number of array element: \n");
scanf("%d", &n);
int a[n];
printf("Insert array element manual or automatic?\nIf manual press 'M' or press 'A' for automatic\n");
scanf("%*c%c", &i_type);
if(i_type == 'a' || i_type == 'A'){
printf("The array elements is:\n");
int i;
for(i=0; i<n; i++){
a[i] = rand()%100;
printf("%d ", a[i]);
}
}else{
printf("Enter %d integer value: ", n);
int i;
for(i=0; i<n; i++){
scanf("%d", &a[i]);
}
printf("The array elements is:\n");
for(i=0; i<n; i++){
printf("%d ", a[i]);
}
}
aSort(a, n);
printf("\nNow the array is in ascending order:\n");
int i;
for(i=0; i<n; i++){
printf("%d ", a[i]);
}
printf("\nEnter the key value which you want to find: ");
int key, numArr, rArr[n];
scanf("%d", &key);
bSearch(a, n, key, rArr, &numArr);
if(numArr == -1){
printf("Item not found!\n");
}else{
int i;
for(i=0; i<=numArr; i++)
printf("Your item found in %d position.\n", rArr[i]+1);
}
return 0;
}
If there are likely to be many repeated values: Do two binary searches, to find the positions of the start and the end of the run of "key" values. Don't terminate the search on exact match, and don't skip over mid; use < for the condition in one search and <= for the other. [The details will be tricky and may need some debugging for cases where the key isn't found or is at one end.]
If there are likely to be few repeated values: Do a single search to find the first occurrence. Then, scan linearly to find the "other end".

Tried to create sorting program, but isn't working as expected

The following code is written for sorting array.
Here the program gives unexpected output results.
The error should be in line 10-19.
#include<stdio.h> //Sorting array program
int main()
{
int arr[20],i,j,n,temp;
printf("Enter number of elements : ");
scanf("%d",&n);
printf("\nEnter the elements of the array : ");
for (i=0;i<n;i++)
scanf("%d",&arr[i]);
for(i=0;i<n;i++)
{
for(j=0;j<arr[i];j++)
if (arr[i+1]<arr[i])
{
temp=arr[i];
arr[i]=arr[i+1];
arr[i+1]=temp;
}
}
printf("\nThe sorted list is : \n");
for (i=0;i<n;i++)
printf("\n arr[%d] : %d",i,arr[i]);
return 0;
}
Output ::
Enter number of elements : 4
Enter the elements of the array : 5
3
2
1
The sorted list is :
arr[0] : 3
arr[1] : 2
arr[2] : 1
arr[3] : -16777216
Process returned 0 (0x0) execution time : 8.161 s
Press ENTER to continue.
Modify your program likes this and you'll find out what happens:
#include <stdio.h> //Sorting array program
#include <assert.h>
int main()
{
int arr[20], i, j, n, temp;
printf("Enter number of elements : ");
scanf("%d", &n);
printf("\nEnter the elements of the array : ");
for (i = 0; i<n; i++)
scanf("%d", &arr[i]);
for (i = 0; i<n; i++)
{ // <<< line added
for (j = 0; j < arr[i]; j++)
{
assert(j < n); // <<< line added
assert(i + 1 < n); // <<< line added
if (arr[i + 1] < arr[i])
{
temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
} // <<< line added
}
printf("\nThe sorted list is : \n");
for (i = 0; i<n; i++)
printf("\n arr[%d] : %d", i, arr[i]);
return 0;
}
Read about assert here.
But anyway the algorithm looks fishy to me anyway:
This line is particularly suspicious:
for (j = 0; j < arr[i]; j++)
Try this
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h> //Sorting array program
int main()
{
int arr[20], i, j, n, temp;
printf("Enter number of elements : ");
scanf("%d", &n);
printf("\nEnter the elements of the array : ");
for (i = 0; i<n; i++)
scanf("%d", &arr[i]);
for (i = 0; i<n; i++)
{
for (j = 0; j<n; j++)
if (arr[i]<arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
printf("\nThe sorted list is : \n");
for (i = 0; i<n; i++)
printf("\n arr[%d] : %d", i, arr[i]);
printf("\n");
return 0;
}

Resources