Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Update 15_InsertSort .c #767

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Max-CoderBoi wants to merge 1 commit into codemistic:main from Max-CoderBoi:patch-3
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 77 additions & 30 deletions Arrays/15_InsertSort .c
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,43 +1,90 @@
#include<stdio.h>
// #include<stdio.h>

struct Array
{
int A[20];
int length;
int size;
};
void Display (struct Array arr)
{
int i;
for (i=0;i<arr.length;i++)
printf("%d ",arr.A[i]);
// struct Array
// {
// int A[20];
// int length;
// int size;
// };
// void Display (struct Array arr)
// {
// int i;
// for (i=0;i<arr.length;i++)
// printf("%d ",arr.A[i]);

}
// }

void InsertSort(struct Array *arr,int x)
{
int i=arr->length-1;
if (arr->length==arr->size) return ;
while (i>=0 && arr->A[i]>x)
{
arr->A[i+1]=arr->A[i];
i--;
}
// void InsertSort(struct Array *arr,int x)
// {
// int i=arr->length-1;
// if (arr->length==arr->size) return ;
// while (i>=0 && arr->A[i]>x)
// {
// arr->A[i+1]=arr->A[i];
// i--;
// }



arr->A[i+1]=x;
// arr->A[i+1]=x;

arr->length++;
// arr->length++;

// }


// int main()

// {
// struct Array arr={{2,3,5,10,15},5,20};
// InsertSort(&arr,1);
// Display(arr);
// return 0;
// }

#include <stdio.h>

struct Array {
int A[20];
int length;
int size;
};

// Function to display array elements
void Display(struct Array arr) {
for (int i = 0; i < arr.length; i++)
printf("%d ", arr.A[i]);
printf("\n");
}

// Function to insert an element in a sorted array
void InsertSort(struct Array* arr, int x) {
if (arr->length == arr->size)
return; // Prevent inserting if array is full

int i = arr->length - 1;

// Shift elements to the right until we find the correct position
while (i >= 0 && arr->A[i] > x) {
arr->A[i + 1] = arr->A[i];
i--;
}

// Insert x at the correct position
arr->A[i + 1] = x;
arr->length++;
}

int main() {
struct Array arr = {{2, 3, 5, 10, 15}, 5, 20}; // Predefined sorted array

printf("Original Array: ");
Display(arr);

InsertSort(&arr, 1); // Insert value into sorted array

int main()
printf("After Insertion: ");
Display(arr);

{
struct Array arr={{2,3,5,10,15},5,20};
InsertSort(&arr,1);
Display(arr);
return 0;
return 0;
}

AltStyle によって変換されたページ (->オリジナル) /