mirror of
https://github.com/TheAlgorithms/C
synced 2025-02-19 23:14:23 +03:00
commit
8e5e6c3820
@ -1,40 +1,62 @@
|
||||
//sorting of array list using bubble sort
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
int* ARRAY=NULL;
|
||||
int ContinueFilling=1; //This is to know if we should continue filling our array
|
||||
int ARRAY_LENGTH=0,isSorted=0,i,TEMPORARY_ELEMENT;
|
||||
|
||||
//This code part is for filling our array
|
||||
while(ContinueFilling){
|
||||
printf("Enter the value number %d \n",ARRAY_LENGTH+1);
|
||||
ARRAY=(int *)realloc(ARRAY,sizeof(int)*(ARRAY_LENGTH));
|
||||
scanf("%d",&ARRAY[ARRAY_LENGTH]);
|
||||
ARRAY_LENGTH+=1;
|
||||
printf("would you enter an other value (1:Continue/0:Sort the actual array)?\n");
|
||||
scanf("%d",&ContinueFilling);
|
||||
/*Displays the array, passed to this method*/
|
||||
void display(int arr[], int n){
|
||||
|
||||
int i;
|
||||
for(i = 0; i < n; i++){
|
||||
printf("%d ", arr[i]);
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
|
||||
}
|
||||
|
||||
//Then we sort it using Bubble Sort..
|
||||
/*Swap function to swap two values*/
|
||||
void swap(int *first, int *second){
|
||||
|
||||
int temp = *first;
|
||||
*first = *second;
|
||||
*second = temp;
|
||||
|
||||
}
|
||||
|
||||
while(!isSorted){ //While our array's not sorted
|
||||
isSorted=1; //we suppose that it's sorted
|
||||
for(i=0;i<ARRAY_LENGTH-1;i++){ //then for each element of the array
|
||||
if(ARRAY[i]>ARRAY[i+1]){ // if the two elements aren't sorted
|
||||
isSorted=0; //it means that the array is not sorted
|
||||
TEMPORARY_ELEMENT=ARRAY[i]; //and we switch these elements using TEMPORARY_ELEMENT
|
||||
ARRAY[i]=ARRAY[i+1];
|
||||
ARRAY[i+1]=TEMPORARY_ELEMENT;
|
||||
/*This is where the sorting of the array takes place
|
||||
arr[] --- Array to be sorted
|
||||
size --- Array Size
|
||||
*/
|
||||
void bubbleSort(int arr[], int size){
|
||||
|
||||
for(int i=0; i<size-1; i++) {
|
||||
for(int j=0; j<size-1; j++) {
|
||||
if(arr[j]>arr[j+1]) {
|
||||
swap(&arr[j], &arr[j+1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
//And we display it
|
||||
for(i=0;i<ARRAY_LENGTH;i++){
|
||||
printf("%d, ",ARRAY[i]);
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, const char * argv[]) {
|
||||
int n;
|
||||
printf("Enter size of array:\n");
|
||||
scanf("%d", &n); // E.g. 8
|
||||
|
||||
printf("Enter the elements of the array\n");
|
||||
int i;
|
||||
int arr[n];
|
||||
for(i = 0; i < n; i++){
|
||||
scanf("%d", &arr[i] );
|
||||
}
|
||||
|
||||
printf("Original array: ");
|
||||
display(arr, n); // Original array : 10 11 9 8 4 7 3 8
|
||||
|
||||
bubbleSort(arr, n);
|
||||
|
||||
printf("Sorted array: ");
|
||||
display(arr, n); // Sorted array : 3 4 7 8 8 9 10 11
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,35 +1,53 @@
|
||||
//sorting of array list using insertion sort
|
||||
#include <stdio.h>
|
||||
#incude <stdlib.h>
|
||||
#define MAX 20
|
||||
|
||||
//i and j act as counters
|
||||
//arraySort is the array that is to be sorted
|
||||
//elmtToInsert will be the element that we will be trying to move to its correct index in the current iteration
|
||||
|
||||
int main()
|
||||
{
|
||||
int i, elmtToInsert , j , arraySort[MAX] = {0};
|
||||
|
||||
for(i = 1 ; i < MAX ; i++) //This array is being sorted in the ascending order.
|
||||
{
|
||||
elmtToInsert = arraySort[i]; //Pick up the ith indexed element of the array. It will be the elmtToInsert.
|
||||
j = i - 1 ;
|
||||
|
||||
while(j >= 0 && elmtToInsert < arraySort[j]) /*compare it with each (i-1)th, (i-2)th... max 0th element, till the correct
|
||||
position of the elmtToInsert, where it is finally greater than the element just
|
||||
before it, is found */
|
||||
{
|
||||
// You'll enter the loop if the elmtToInsert is less than the element just before it.
|
||||
|
||||
arraySort[j+1] = arraySort[j]; //shift the current element one place forward to create room for insertion of the elmtToInsert
|
||||
j--;
|
||||
}
|
||||
//when we exit the loop, j+1 will be the index of the correct position of the elmtToInsert
|
||||
|
||||
arraySort[j+1] = elmtToInsert ; //'insert' the elmtToInsert into its correct position
|
||||
|
||||
}
|
||||
/*Displays the array, passed to this method*/
|
||||
void display(int arr[], int n){
|
||||
|
||||
int i;
|
||||
for(i = 0; i < n; i++){
|
||||
printf("%d ", arr[i]);
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
/*This is where the sorting of the array takes place
|
||||
arr[] --- Array to be sorted
|
||||
size --- Array Size
|
||||
*/
|
||||
void insertionSort(int arr[], int size){
|
||||
int j,temp,i;
|
||||
for(i=0; i<size; i++) {
|
||||
temp = arr[(j=i)];
|
||||
while(--j >= 0 && temp < arr[j]) {
|
||||
arr[j+1] = arr[j];
|
||||
arr[j] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, const char * argv[]) {
|
||||
int n;
|
||||
printf("Enter size of array:\n");
|
||||
scanf("%d", &n); // E.g. 8
|
||||
|
||||
printf("Enter the elements of the array\n");
|
||||
int i;
|
||||
int arr[n];
|
||||
for(i = 0; i < n; i++){
|
||||
scanf("%d", &arr[i] );
|
||||
}
|
||||
|
||||
printf("Original array: ");
|
||||
display(arr, n); // Original array : 10 11 9 8 4 7 3 8
|
||||
|
||||
insertionSort(arr, n);
|
||||
|
||||
printf("Sorted array: ");
|
||||
display(arr, n); // Sorted array : 3 4 7 8 8 9 10 11
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -1,89 +1,64 @@
|
||||
//sorting of linked list using selection sort
|
||||
#include<stdio.h>
|
||||
struct node
|
||||
{
|
||||
int info;
|
||||
struct node *link;
|
||||
};
|
||||
struct node *start=NULL;
|
||||
//func to create node
|
||||
struct node *createnode()
|
||||
{
|
||||
struct node *p;
|
||||
p=(struct node*)malloc(sizeof(struct node));
|
||||
return(p);
|
||||
//sorting of array list using selection sort
|
||||
#include <stdio.h>
|
||||
|
||||
/*Displays the array, passed to this method*/
|
||||
void display(int arr[], int n){
|
||||
|
||||
int i;
|
||||
for(i = 0; i < n; i++){
|
||||
printf("%d ", arr[i]);
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
|
||||
}
|
||||
//program to insert at begining
|
||||
void insert()
|
||||
{struct node *t;
|
||||
t=createnode();
|
||||
printf("\nenter the value to insert");
|
||||
scanf("%d",&t->info);
|
||||
if(start==NULL)
|
||||
{start=t;
|
||||
}
|
||||
else
|
||||
{strutc node *p;
|
||||
p=start;
|
||||
t->link=p;
|
||||
start=t;
|
||||
}
|
||||
//program to sort the linked list using selection sort
|
||||
void sort()
|
||||
{
|
||||
struct node *p,*t;
|
||||
t=start;
|
||||
int tmp;
|
||||
for(t=start;t->link!=NULL;t=t->link)
|
||||
{
|
||||
for(p=t->link;p!=NULL;p=p->link)
|
||||
{
|
||||
if(t->info>p->info)
|
||||
tmp=t->info;
|
||||
t->info=p->info;
|
||||
p->info=tmp;
|
||||
}
|
||||
}
|
||||
//program to view sorted list
|
||||
void viewlist()
|
||||
{
|
||||
struct node *p;
|
||||
if(start==NULL)
|
||||
{
|
||||
printf("\nlist is empty");
|
||||
}
|
||||
else
|
||||
{
|
||||
p=start;
|
||||
while(p!=NULL)
|
||||
{
|
||||
printf("%d",p->info);
|
||||
p=p->link;
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
whhile(1)
|
||||
{
|
||||
printf("\n1.insert value at beg");
|
||||
printf("\n2.sort the list");
|
||||
printf("\n3.view value");
|
||||
printf("\nenter your choice");
|
||||
scanf("%d",&n);
|
||||
switch(n)
|
||||
{case 1:
|
||||
insert();
|
||||
break;
|
||||
case 2:
|
||||
sort();
|
||||
break;
|
||||
case 3:
|
||||
viewlist();
|
||||
break;
|
||||
default:
|
||||
printf("\ninvalid choice");
|
||||
}
|
||||
}
|
||||
return(0);
|
||||
}
|
||||
|
||||
/*Swap function to swap two values*/
|
||||
void swap(int *first, int *second){
|
||||
|
||||
int temp = *first;
|
||||
*first = *second;
|
||||
*second = temp;
|
||||
|
||||
}
|
||||
|
||||
/*This is where the sorting of the array takes place
|
||||
arr[] --- Array to be sorted
|
||||
size --- Array Size
|
||||
*/
|
||||
void selectionSort(int arr[], int size){
|
||||
|
||||
for(int i=0; i<size; i++) {
|
||||
int min_index = i;
|
||||
for(int j=i+1; j<size; j++) {
|
||||
if(arr[min_index] > arr[j]) {
|
||||
min_index = j;
|
||||
}
|
||||
}
|
||||
swap(&arr[i], &arr[min_index]);
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, const char * argv[]) {
|
||||
int n;
|
||||
printf("Enter size of array:\n");
|
||||
scanf("%d", &n); // E.g. 8
|
||||
|
||||
printf("Enter the elements of the array\n");
|
||||
int i;
|
||||
int arr[n];
|
||||
for(i = 0; i < n; i++){
|
||||
scanf("%d", &arr[i] );
|
||||
}
|
||||
|
||||
printf("Original array: ");
|
||||
display(arr, n); // Original array : 10 11 9 8 4 7 3 8
|
||||
|
||||
selectionSort(arr, n);
|
||||
|
||||
printf("Sorted array: ");
|
||||
display(arr, n); // Sorted array : 3 4 7 8 8 9 10 11
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user