Problem Statement
Sorting
One common task for computers is to sort data. For example, people might want to see all their files on a computer sorted by size. Since sorting is a simple problem with many different possible solutions, it is often used to introduce the study of algorithms.
Insertion Sort
These challenges will cover Insertion Sort, a simple and intuitive sorting algorithm. We will first start with an already sorted list.
Insert element into sorted list
Given a sorted list with an unsorted numberV in the rightmost cell, can you write some simple code to insert V into the array so that it remains sorted?
Print the array every time a value is shifted in the array until the array is fully sorted. The goal of this challenge is to follow the correct order of insertion sort.
Guideline: You can copy the value ofV
to a variable and consider its cell "empty". Since this leaves an extra
cell empty on the right, you can shift everything over until V can be inserted. This will create a duplicate of each value, but when you reach the right spot, you can replace it with V .
Input Format
There will be two lines of input:
On each line, output the entire array every time an item is shifted in it.
Constraints
1≤s≤1000
−10000≤V≤10000,V∈ar
Sample Input
3 is removed from the end of the array.
In the1 st line 8>3 , so 8 is shifted one cell to the right.
In the2 nd line 6>3 , so 6 is shifted one cell to the right.
In the3 rd line 4>3 , so 4 is shifted one cell to the right.
In the4 th line 2<3 , so 3 is placed at position 2 .
Task
Complete the method insertionSort which takes in one parameter:
In the next Challenge, we will complete the insertion sort itself!
MY SOLUTION:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <assert.h>
int main(void) {
int t,i,j,x;
scanf("%d",&t);
long int a[t],temp;
for(i=0;i<t;i++)
scanf("%ld",&a[i]);
for(i=0;i<t;i++)
{
temp=a[i];
j=i-1;
while((temp<a[j])&&(j>=0)){
a[j+1]=a[j];
j=j-1;
for(x=0;x<t;x++)
printf("%ld ",a[x]);
printf("\n");
}
a[j+1]=temp;
}
for(x=0;x<t;x++)
printf("%ld ",a[x]);
printf("\n");
return 0;
}
One common task for computers is to sort data. For example, people might want to see all their files on a computer sorted by size. Since sorting is a simple problem with many different possible solutions, it is often used to introduce the study of algorithms.
Insertion Sort
These challenges will cover Insertion Sort, a simple and intuitive sorting algorithm. We will first start with an already sorted list.
Insert element into sorted list
Given a sorted list with an unsorted number
Print the array every time a value is shifted in the array until the array is fully sorted. The goal of this challenge is to follow the correct order of insertion sort.
Guideline: You can copy the value of
Input Format
There will be two lines of input:
s - the size of the arrayar - the sorted array of integers
On each line, output the entire array every time an item is shifted in it.
Constraints
Sample Input
5
2 4 6 8 3
Sample Output2 4 6 8 8
2 4 6 6 8
2 4 4 6 8
2 3 4 6 8
ExplanationIn the
In the
In the
In the
Task
Complete the method insertionSort which takes in one parameter:
ar - an array with the valueV in the right-most cell.
In the next Challenge, we will complete the insertion sort itself!
MY SOLUTION:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <assert.h>
int main(void) {
int t,i,j,x;
scanf("%d",&t);
long int a[t],temp;
for(i=0;i<t;i++)
scanf("%ld",&a[i]);
for(i=0;i<t;i++)
{
temp=a[i];
j=i-1;
while((temp<a[j])&&(j>=0)){
a[j+1]=a[j];
j=j-1;
for(x=0;x<t;x++)
printf("%ld ",a[x]);
printf("\n");
}
a[j+1]=temp;
}
for(x=0;x<t;x++)
printf("%ld ",a[x]);
printf("\n");
return 0;
}