Skip to content
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@

// input: 1 2 3 4 5 6 -1

// output:

// 1 2 3 4 5 6 -1
// 1 2 3 4 5 6
// the length of the linked list is:6

// code begin :

#include <bits/stdc++.h>
using namespace std;

class node{
public:
int data ;
node*next ;
node (int data ){
this->data = data ;
next = NULL ;
}
};

// used to take the input of the node

node* takeinput(){
int data ;
cin>>data;
node*head = NULL;
while(data!=-1){
node*newnode = new node(data);
if(head==NULL){
head = newnode;
}
else {
node*temp = head;
while(temp->next!=NULL){
temp = temp->next;

}
temp->next =newnode;



}
cin>>data;
}
return head;
}

// used to print the full linked list
void print(node*head){
while(head!=NULL){
cout<<head->data<<" ";
head = head->next;

}
}

int length(node*head ,int l){
if(head==NULL) return l; // base case
// int l;

return length(head->next,l+=1); // recurrsive call
}

int main(){
// freopen("in.txt", "r", stdin);
// freopen("outp1.txt", "w", stdout);

node*head = takeinput();
print(head);
cout<<endl;
cout<<"the length of the linked list is:";
cout<<length(head,0);
}
61 changes: 61 additions & 0 deletions dsa-roadmaps/Beginners/Algorithms/Sorting/bubble_sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Optimized implementation of Bubble sort


// output:
// Sorted array:
// 11 12 22 25 34 64 90

#include <bits/stdc++.h>
using namespace std;
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}

// An optimized version of Bubble Sort
void bubbleSort(int arr[], int n)
{
int i, j;
bool swapped;
for (i = 0; i < n-1; i++)
{
swapped = false;
for (j = 0; j < n-i-1; j++)
{
if (arr[j] > arr[j+1])
{
swap(&arr[j], &arr[j+1]);
swapped = true;
}
}

// IF no two elements were swapped by inner loop, then break
if (swapped == false)
break;
}
}

/* Function to print an array */
void printArray(int arr[], int size)
{
int i;
for (i = 0; i < size; i++)
cout <<" "<< arr[i];
cout <<" n";
}

// Driver program to test above functions
int main()
{
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr)/sizeof(arr[0]);
bubbleSort(arr, n);
cout <<"Sorted array: \n";
printArray(arr, n);
return 0;
}


// This code is contributed by shivanisinghss2110